Search code examples
androiduser-interfaceandroid-viewpagerandroid-fragmentactivityviewpagerindicator

Android ViewPager with bottom dots


I want to add 3 bottom dots to my ViewPager, like this.

3 bottom dots 3 bottom dots 3 bottom dots

I use FragmentActivity and support library ViewPager.


Solution

  • Pager bottom dots/indicators in Compose

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun PageIndicator(
        count: Int,
        pagerState: PagerState,
        modifier: Modifier = Modifier
    ) {
        Row(
            modifier = modifier
        ) {
            repeat(count) { index ->
    
                // play with size and corner shape to get the desired result
                // following displays a dot
                Box(
                    modifier = Modifier
                        .size(16.dp)
                        .clip(RoundedCornerShape(50))
                        .background(
                            if (pagerState.currentPage == index) selectedColour else normalColour
                        )
                )
    
                // to add space between indicators
                if (index != count) {
                    Spacer(modifier = Modifier.padding(horizontal = 4.dp))
                }
            }
        }
    }
    

    That's it :) Compose is love :D

    Previous answer for ViewPager

    No need for that much code.

    You can do all this stuff without coding so much by using only viewpager with tablayout.

    Your main Layout:

    <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
    
       <android.support.v4.view.ViewPager
           android:id="@+id/pager"
           android:layout_width="match_parent"
           android:layout_height="match_parent">
    
       </android.support.v4.view.ViewPager>
       <android.support.design.widget.TabLayout
           android:id="@+id/tabDots"
           android:layout_alignParentBottom="true"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           app:tabBackground="@drawable/tab_selector"
           app:tabGravity="center"
           app:tabIndicatorHeight="0dp"/>
    </RelativeLayout>
    

    Hook up your UI elements in activity or fragment as follows:

    Java Code:

    mImageViewPager = (ViewPager) findViewById(R.id.pager);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
    tabLayout.setupWithViewPager(mImageViewPager, true);
    

    That's it, you are good to go.

    You will need to create the following xml resource file in the drawable folder.

    tab_indicator_selected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        android:innerRadius="0dp"
        android:shape="ring"
        android:thickness="4dp"
        android:useLevel="false"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/colorAccent"/>
    </shape>
    

    tab_indicator_default.xml

    <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:innerRadius="0dp"
                android:shape="oval"
                android:thickness="2dp"
                android:useLevel="false">
                <solid android:color="@android:color/darker_gray"/>
        </shape>
    

    tab_selector.xml

     <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/tab_indicator_selected"
              android:state_selected="true"/>
    
        <item android:drawable="@drawable/tab_indicator_default"/>
    </selector>
    

    Feeling as lazy as I am? Well, all the above code is converted into a library! Usage Add the following in your gradle: implementation 'com.chabbal:slidingdotsplash:1.0.2' Add the following to your Activity or Fragment layout.

    <com.chabbal.slidingdotsplash.SlidingSplashView
            android:id="@+id/splash"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:imageResources="@array/img_id_arr"/>
    

    Create an integer array in strings.xml e.g.

    <integer-array name="img_id_arr">
       <item>@drawable/img1</item>
       <item>@drawable/img2</item>
       <item>@drawable/img3</item>
       <item>@drawable/img4</item>
    </integer-array>
    

    Done! Extra in order to listen page changes use addOnPageChangeListener(listener); Github link.