Can I change the length of the indicator inside the TabLayout according to the length of the text?

129 Views Asked by At

I am practicing to create a TabLayout and connect it to the ViewPager. The following tabs have been added to TabLayout.

MainActivity.kt

tabLayout.addTab(tabLayout.newTab().setText("A"))
tabLayout.addTab(tabLayout.newTab().setText("BBBB"))
tabLayout.addTab(tabLayout.newTab().setText("CC"))
tabLayout.addTab(tabLayout.newTab().setText("DDD"))
tabLayout.addTab(tabLayout.newTab().setText("EEEEE"))

As you can see from the code above, the length of text added to TabLayout is all different. I've customized the indicators to be used on the tabs myself.

custom_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="5dp"
        android:right="5dp">
        <shape android:shape="rectangle">
            <solid android:color="#EBEBF0" />
            <corners android:radius="20dp" />
            <size android:height="32dp" />
        </shape>
    </item>
</layer-list>

activity_main.xml

 <com.google.android.material.tabs.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="25dp"
    android:layout_marginRight="10dp"
    android:overScrollMode="never"
    app:tabGravity="fill"
    app:tabIndicator="@drawable/custom_indicator"
    app:tabIndicatorColor="#EBEBF0"
    app:tabInlineLabel="true"
    app:tabIndicatorGravity="center"
    app:tabMode="fixed" />

To talk about the length of characters in Tab again, I want to give the custom indicator length differently depending on the length of characters in TabLayout. Currently, in that situation, the indicator fits in the same size regardless of the length of the letter. How can I change the indicator length differently for each character length??

0

There are 0 best solutions below