How to show a circle tab indicator along Viewpager?
Asked Answered
M

3

5

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.

Moony answered 22/7, 2017 at 14:43 Comment(9)
there is one library want to use?Querist
yeah tell me. It may be useful.Moony
ok,wait a minuteQuerist
check this : dubedout.eu/2016/11/13/…Querist
@shweta it seems useful, am checking on it. Thanks for helping out.Moony
below answer also works I done with bothQuerist
check this also useful for you if you don't want to use a library: xleon.net/xamarin/android/…Querist
#38459809 see the accepted answerCounterespionage
At least take the time to search the web before stating question which already have an answerCounterespionage
M
6

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"/>
Melan answered 22/7, 2017 at 15:4 Comment(1)
This is overlapping the tab title. Any idea to the dot below the tab title?Jahvist
S
4

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"

Smectic answered 11/9, 2019 at 6:57 Comment(0)
S
0

Here's how I achieved it

tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
            android:width="8dp"
            android:height="8dp"
            android:bottom="16dp"
            android:end="8dp"
            android:gravity="center"
            android:start="8dp"
            android:top="8dp">
        
        <shape android:shape="rectangle">
            <size android:width="8dp" android:height="8dp" />
            <corners android:radius="4dp" />
            <solid android:color="?attr/colorAccent" />
        </shape>
    </item>
</layer-list>

Important thing is to have fixed width and height.

Usage:

<com.google.android.material.tabs.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabIndicatorHeight="8dp"
                app:tabIndicator="@drawable/tab_indicator" />

Screed answered 26/3, 2021 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.