How to Implement ViewPager with Dotsindicator in Android?
Last Updated :
18 Feb, 2022
ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in-app. It is a widget found in the support library.
What are Dotsindicator?
These are dots that help us to see which view is currently opened when we have multiple views. Some of the attributes of the dots indicator are as follows-
Attribute
| Description
|
---|
dotsColor | Color of the dots |
selectedDotColor | Color of the selected dot (by default the color of the dot) |
progressMode | Lets the selected dot color to the dots behind the current one |
dotsSize | Size in dp of the dots (by default 16dp) |
dotsSpacing | Size in dp of the space between the dots (by default 4dp) |
dotsWidthFactor | The dots scale factor for page indication (by default 2.5) |
dotsCornerRadius | The dots corner radius (by default the half of dotsSize for circularity) |
What we are going to build in this article?
In this article, we will see three different kinds of dots indicators which come into action when we change the images in an image view. Here is a sample video of what we are going to build in this article. Note that we going to build this application in the Java language.
Step by Step Implementation
Step 1: Create a new project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- You can change the name of the project at your convenience.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2. Adding required dependency
Navigate to Gradle scripts > build.gradle(module) and use the following dependency in it-
implementation 'com.tbuonomo.andrui:viewpagerdotsindicator:3.0.3'
Step 3. Working on XML files
Navigate to app > res > layout > activity_main.xml file and use the following code in it-
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/view_pager"/>
<com.tbuonomo.viewpagerdotsindicator.DotsIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dot1"
android:layout_marginTop="10dp"
app:dotsColor="@android:color/holo_green_light"
app:selectedDotColor="@android:color/holo_green_dark"
app:dotsSize="15dp"
app:dotsSpacing="5dp"
app:progressMode="true"/>
<com.tbuonomo.viewpagerdotsindicator.SpringDotsIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dot2"
android:layout_marginTop="10dp"
app:dotsColor="@android:color/holo_orange_light"
app:selectedDotColor="@android:color/holo_orange_dark"
app:dotsSize="15dp"
app:dotsSpacing="5dp"
app:stiffness="300"/>
<com.tbuonomo.viewpagerdotsindicator.WormDotsIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dot3"
android:layout_marginTop="10dp"
app:dotsColor="@android:color/holo_blue_light"
app:selectedDotColor="@android:color/holo_blue_dark"
app:dotsSize="15dp"
app:dotsSpacing="5dp"/>
</LinearLayout>
Navigate to app > res > layout > right-click > new > layout resource file and it as item. Use the following code in item.xml file-
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image_view"
android:adjustViewBounds="true"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4. Working on Java files
Go to the MainActivity.java file and use the following code in it-
Java
package com.example.viewpagerwithdotsindicator;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.tbuonomo.viewpagerdotsindicator.DotsIndicator;
import com.tbuonomo.viewpagerdotsindicator.SpringDotsIndicator;
import com.tbuonomo.viewpagerdotsindicator.WormDotsIndicator;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
DotsIndicator dot1;
SpringDotsIndicator dot2;
WormDotsIndicator dot3;
ViewAdapter viewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=findViewById(R.id.view_pager);
dot1=findViewById(R.id.dot1);
dot2=findViewById(R.id.dot2);
dot3=findViewById(R.id.dot3);
viewAdapter=new ViewAdapter(this);
viewPager.setAdapter(viewAdapter);
dot1.setViewPager(viewPager);
dot2.setViewPager(viewPager);
dot3.setViewPager(viewPager);
}
}
Navigate to app > right-click > new > java class and name it as ViewAdapter. Use the following code in ViewAdapter.java file-
Java
package com.example.viewpagerwithdotsindicator;
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
public class ViewAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Integer[] images={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};
public ViewAdapter(Context context)
{
this.context=context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view==object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater=(LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE
);
View view =layoutInflater.inflate(R.layout.item,null);
ImageView imageView=view.findViewById(R.id.image_view);
imageView.setImageResource(images[position]);
ViewPager viewPager=(ViewPager) container;
viewPager.addView(view,0);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
ViewPager viewPager=(ViewPager) container;
View view=(View) object;
viewPager.removeView(view);
}
}
Here is the final output of our application.
Output:
Similar Reads
How to Implement Stepper Functionality in Android?
A Stepper widget allows you to organize content in a sequence of steps, and the user can navigate between them. In this article we are going to implement a Stepper-like functionality in an Android app here we take the help of Tablayout to achieve this. A Sample video is attached below to get an idea
4 min read
How to Implement OTP View in Android?
An OTP View or PinView in android is a widget that allows users to enter their PIN, OTP, etc. They are generally used for two-factor authentication or phone number verification by OTP. A sample video is given below to get an idea about what we are going to do in this article. Note: In order to imple
2 min read
ViewPager Using Fragments in Android with Example
ViewPager is a layout manager that allows the user to flip left and right through pages of data. It is a depreciated concept we are using ViewPager2 now. It is mostly found in apps like Youtube, Snapchat where the user shifts right-left or top-bottom to switch to a screen. Instead of using activitie
6 min read
ViewPager2 in Android with Example
Before going to Viewpager2 let us know about Viewpager. Most of you have used WhatsApp, when you open WhatsApp you can see at the top, there are four sections Camera, Chats, Status, Calls these are the Viewpager. So a Viewpager is an android widget that is used to navigate from one page to another p
6 min read
How to Implement Circular ProgressBar in Android?
ProgressBar is used when we are fetching some data from another source and it takes time, so for the user's satisfaction, we generally display the progress of the task. In this article, we are going to learn how to implement the circular progress bar in an Android application using Java. So, this ar
6 min read
Cube in Scaling Animation with ViewPager in Android
The Android ViewPager has become a very interesting concept among Android apps. It enables users to switch smoothly between fragments which have a common UI and itâs the best way to make your app extraordinary from others. ViewPagers provide visual continuity. They basically keep track of which page
5 min read
How to Implement TextSwitcher in Android?
In Android, TextSwitcher is a useful tool for displaying text with animations. It can be seen as a special version of ViewSwitcher, where its children are only TextView elements. TextSwitcher allows us to add smooth transitions to text, making it part of the transition widget family. Whenever you ca
3 min read
How to implement View Shaker in Android
View Shaker is an animation in which, the UI of screen vibrates for a limited period of time. This can be implemented on the whole layout or some particular widget. It is a very common effect that developers use, especially to show incorrect credentials. View Shaker helps us to animate the widgets.
3 min read
How to Implement TabLayout with Icon in Android?
TabLayout is used to implement horizontal tabs. TabLayout is introduced in the design support library to implement tabs. Tabs are created using the newTab() method of TabLayout class. The title and icon of Tabs are set through setText(int) and setIcon(int) methods of TabListener interface respective
5 min read
How to Implement PDF Picker in Android?
In this article, we are going to see how we can implement a PDF picker in android studio and get the file information of the pdf like file name, size and path. In this application, we will create a launcher to launch the file picker and display the file information after a pdf is picked successfully
5 min read