Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a circle tab indicator along Viewpager?

I want to show a tabindicator as in the image below.

tabIndicator

I am using viewpager. I already tried TabLayout with viewpager, in which we can only adjust tab indicator color and height. How can I implement this.

like image 263
Jerin A Mathews Avatar asked Jul 22 '17 14:07

Jerin A Mathews


2 Answers

when seleted 'tab_selected.xml'

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape
            android:shape="ring"
            android:useLevel="false"
            android:thickness="4dp"
            android:innerRadius="0dp">
            <solid android:color="#000"/>
        </shape>
    </item>

</selector>

when not selected 'tab_unselected'

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item>
        <shape
            android:shape="ring"
            android:useLevel="false"
            android:thickness="4dp"
            android:innerRadius="0dp">
            <solid android:color="#3b000000"/>
        </shape>
    </item>



</selector>

now when state_presses is true or not 'tab'

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:drawable="@drawable/tab_selected"
          android:state_selected="true"/>


    <item android:drawable="@drawable/tab_unseleted"/>



</selector>

now in tablayout

<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tablayout"
    android:layout_alignParentBottom="true"
    app:tabIndicatorHeight="0dp"
    app:tabGravity="center"
    app:tabBackground="@drawable/tab"/>
like image 72
Salman500 Avatar answered Nov 12 '22 14:11

Salman500


Keep it simple and just use one XML file.

tab_indicator_circle.xml

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

    <size
        android:width="4dp"
        android:height="4dp" />

    <solid android:color="@color/colorAccent" />
</shape>

Usage:

<com.google.android.material.tabs.TabLayout
    android:id="@+id/myTabLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:tabIndicator="@drawable/tab_indicator_circle"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:tabIndicatorHeight="10dp"
    app:tabGravity="center" />  

Note that I'm using TabLayout from
implementation "com.google.android.material:material:1.1.0-alpha10"

like image 34
Markymark Avatar answered Nov 12 '22 16:11

Markymark