The Wayback Machine - https://web.archive.org/web/20240913175027/https://www.geeksforgeeks.org/how-to-implement-viewpager-with-dotsindicator-in-android/
Open In App

How to Implement ViewPager with Dotsindicator in Android?

Last Updated : 18 Feb, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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: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 
    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 a TabLayout in Android using ViewPager and Fragments
In this article, we will learn about how to add TabLayout with ViewPager in an app. TabLayout provides a horizontal layout to display tabs. If TabLayout is used then along with it, Fragment is also used, because fragments are light weight and the app can have more functionality on a single screen if more fragments are added. Whenever the user click
3 min read
How to Implement Tabs, ViewPager and Fragment in Android using Kotlin?
In some Android apps, Tabs are used, which allows developers to combine multiple tasks (operations) on a single activity. On the other hand, it provides a different look to that app. It is also possible to provide a different feel like left and right swipes by using ViewPager. And to implement this topic, a few terms are required such as ViewPager,
8 min read
Android Image Slider using ViewPager in Kotlin
Image slider is seen in most e-commerce applications that display advertisements on the home screen. This slider displays the advertisement banners which users can slide to view the others. In this article, we will take a look at Implementing the image slider using ViewPager in Android using Kotlin. A sample video is given below to get an idea abou
5 min read
Android ViewPager in Kotlin
ViewPager adds a sleek, swipeable interface that lets users effortlessly navigate between pages, just like flipping through a book. This feature is common in social media apps; for instance, WhatsApp greets you with three intuitive tabs: chats, status, and calls. This makes the app look cool. You've probably noticed it guiding you through an app’s
5 min read
Image Slider in Android using ViewPager
When talking about Android Apps, the first thing that comes to mind is variety. There are so many varieties of Android apps providing the user with beautiful dynamic UI. One such feature is to navigate in the Android Apps using the left and right swipes as opposed to clicking on Buttons. Not only does it look more simple and elegant but also provid
4 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 is visible and then ask PageAdapter to display th
5 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 mostly found in apps like Youtube, Snapchat where the user shifts right - left to switch to a screen. Instead of using activities fragments are used. It is also used to guide the user through the app when the user launches the app for the first ti
7 min read
How to Implement Google Map Inside Fragment in Android?
In Android, the fragment is the part of Activity that represents a portion of the User Interface(UI) on the screen. It is the modular section of the android activity that is very helpful in creating UI designs that are flexible in nature and auto-adjustable based on the device screen size. The UI flexibility on all devices improves the user experie
4 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. Various effects can be added to it. Effects Attent
3 min read
How to implement Picture in Picture (PIP) in Android?
In this article, it is explained how to implement a Picture in Picture (PIP) in an Android application.We have seen in many apps such as Google Maps while using navigation that when we close the app, there is a floating screen that appears at the bottom right of the screen as shown in the image below. This screen is known as PIP(Picture in Picture)
3 min read
How to Implement RecyclerView in a Fragment in Android?
In Android, a fragment is a modular section of a user interface that can be combined with other fragments to create a flexible and responsive application. A fragment represents a behavior or a portion of the user interface in an Activity, which can be reused in different activities or layouts. It has its own lifecycle and can receive input events a
12 min read
Implement Zoom In or Zoom Out in Android
Zoom In and Zoom Out animations are used to enlarge and reduce the size of a view in Android applications respectively. These types of animations are often used by developers to provide a dynamic nature to the applications. Users also feel the changes happening in the application by watching these kinds of animations. XML Attributes of Scale Tag Th
4 min read
How to Implement Swipe Down to Refresh in Android
Certain applications show real-time data to the users, such as stock prices, availability of a product on online stores, etc. Showing real-time data requires continuous syncing of the application, and could be possible by implementing a program such as a thread. A Thread could be initiated by the application and update the real-time information imp
4 min read
How to Implement YoutubePlayerView Library in Android?
If you are looking to display YouTube videos inside your app without redirecting your user from your app to YouTube then this library is very helpful for you to use. With the help of this library, you can simply play videos from YouTube with the help of a video id inside your app itself without redirecting your user to YouTube. Now we will see the
8 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 respectively. Tabs of layout are attached over TabLayout usi
4 min read
How to Implement Preferences Settings Screen in Android?
In many apps, we have seen the Settings screen which is most common in most of the apps. This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen. In this article, we will take a look at implementing the preferences setting screen in Andro
7 min read
How to Implement Modal Message Layout of Firebase In-App Messaging in Android?
Prerequisite: How to Push Notification in Android using Firebase In-App Messaging? We have seen the implementation of Firebase In-App Messaging in Android. In that article, we have implemented the Card Message Layout. In this article, we will take a look at the implementation of the Modal Message Layout. A sample image is given below to get an idea
3 min read
How to Implement Image only Message Layout of Firebase In-App Messaging in Android?
Prerequisite: How to Push Notification in Android using Firebase In-App Messaging? We have seen the implementation of Firebase In-App Messaging in Android. In that article, we have implemented the Card Message Layout. In this article, we will take a look at the implementation of the Image only Message Layout. A sample image is given below to get an
3 min read
How to Implement Top banner Message Layout of Firebase In-App Messaging in Android?
Prerequisite: How to Push Notification in Android using Firebase In-App Messaging? We have seen the implementation of Firebase In-App Messaging in Android. In that article, we have implemented the Card Message Layout. In this article, we will take a look at the implementation of the Top banner Message Layout. A sample image is given below to get an
3 min read
How to Implement SuperBottomBar in Android App?
In this article, we are going to implement bottom navigation like there in Spotify. We all have come across apps that have a Bottom Navigation Bar. Some popular examples include Instagram, Snapchat, etc. In this article, let’s learn how to implement an easy stylish SuperBottomBar functional Bottom Navigation Bar in the Android app. For Creating a B
3 min read
How to Implement Dynamic TabLayout in Android?
In this article, we will learn about how to add Dynamic TabLayout with ViewPager in an app. TabLayout provides a horizontal layout to display tabs. Here we will be just specifying the number of tabs we want to have. WhatsApp, Facebook, etc are a very good example of TabLayout with ViewPager. A sample GIF is given below to get an idea about what we
5 min read
Implement Different Types of AlertBox using Alerter Library in Android
In this article, we are going to implement a very important feature related to the AlertBox. Usually, we create an AlertBox to show some important content. Here we are going to learn how to implement that feature in our Android App using Alert Library. A sample video is given below to get an idea about what we are going to do in this article. Note
5 min read
How to Implement Google's Places AutocompleteBar in Android?
If you ever used Google Maps on mobile or accessed from a desktop, you must have definitely typed in some location into the search bar and selected one of its results. The result might have had fields such as an address, phone numbers, ratings, timings, etc. Moreover, if you ever searched for a place on google.com from a desktop, you must have got
5 min read
How to Implement Current Location Button Feature in Google Maps in Android?
The current location is a feature on Google Maps, that helps us locate the device's position on the Map. Through this article, while we will be implementing Google Maps, we shall also be implementing a button, which will fetch our current location and navigate it on the map. Note that we are going to implement this project using the Kotlin language
5 min read
How to Implement Custom Dialog Maker in Android?
In this article, we are going to make an application of Custom Dialog Maker in android studio. In this application, we can create dialogs of our own choice of style, type, and animation. What is a dialog? A dialog is a small window that prompts the user to make a decision, provide some additional information, and inform the user about some particul
5 min read
How to Implement MultiSelect DropDown in Android?
In this article, we are going to see how we can make a MultiSelect DropDown in android studio and will select multiple items from a dropdown list. Advantages of MultiSelect DropDown. It is a good replacement for list boxes as it uses less space does the same work as a list box and gives a good look to UI.Dropdown lists are familiar selection mechan
4 min read
Android Jetpack Compose - Implement Different Types of Styles and Customization to Text
Jetpack Compose is a new UI toolkit from Google used to create native Android UI. It speeds up and simplifies UI development using less code, Kotlin APIs, and powerful tools. Prerequisites:Familiar with Kotlin and OOP Concepts as wellBasic understanding of Jetpack ComposeAndroid Studio Canary Version A sample video is given below to get an idea abo
14 min read
Implement Caching in Android Using RxJava Operators
The cache on your Android phone is a collection of little pieces of information that your apps and web browser utilize to improve efficiency. RxOperator is essentially a function that specifies the observable, as well as how and when it should emit the data stream. In RxJava, there are hundreds of operators to choose from. The majority of operators
5 min read
How to Implement Offline Caching using NetworkBoundResource in Android?
Almost, every android application that requires fetching data over a network, needs caching. First, let understand What does caching means? Most of us have used applications that require the data to be fetched from the web. Such an application with an offline-first architecture will always try to fetch the data from the local storage. On the other
11 min read
How to Implement Notification Counter in Android?
Notification Counter basically counts the notifications that you got through an application and shows on the top of your application icon so that you get to know you get new messages or any new update without opening your application or specific feature like the message button in Instagram. Notification counter is a feature that is provided in almo
7 min read