I am trying to set custom drawable as tabIndicator as shown below
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
app:tabIndicatorFullWidth="false"
app:layout_scrollFlags="scroll"
app:tabIndicator="@drawable/tab_layout_indicator_selected"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
drawable/tab_layout_indicator_selected
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="2dp" />
<gradient
android:angle="180"
android:endColor="#FF00FF"
android:startColor="#00FFFF"
android:type="linear" />
<size
android:width="60dp"
android:height="4dp" />
</shape>
</item>
<item android:gravity="end">
<shape android:shape="oval">
<solid android:color="#FF00FF" />
<size
android:width="4dp"
android:height="4dp" />
</shape>
</item>
</layer-list>
This shows shape of indicator as my drawable but its color remains always gray, not as given in drawable.
This example demonstrates how do I create a Tab Layout in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 3 − Add the following code to res/layout/activity_main. xml.
To display the tab, you need to add it to the layout via one of the addTab(Tab) methods. For example: TabLayout tabLayout = ...; tabLayout. addTab(tabLayout.
TabLayout provides a horizontal layout to display tabs. Population of the tabs to display is done through TabLayout.Tab instances. You create tabs via newTab ().
Using tabs 1 Basic usage 2 Making tabs accessible. The Android tab components support screen reader descriptions for tabs and badges . ... 3 Using tabs with ViewPager. Dynamically create TabItem s based on the number of pages, their titles, etc. 4 Using tabs with ViewPager2. ... 5 Adding badges to tabs. ...
Simply add com.google.android.material.tabs.TabLayout, which will be used for rendering the different tab options. The androidx.viewpager.widget.ViewPager component will be used to page between the various fragments we will create. Now that we have the ViewPager and our tabs in our layout, we should start defining the content of each of the tabs.
Otherwise, the indicator will not be shown unless gravity is set to INDICATOR_GRAVITY_STRETCH, in which case it will ignore indicator height and stretch across the entire height and width of the TabLayout. This defaults to INDICATOR_GRAVITY_BOTTOM if not set. This method is deprecated.
I had the same issue when using a custom drawable. What fixed it for me was setting the tab indicator height.
app:tabIndicatorHeight="2dp"
There is a separate attribute for the tab indicator color:
<com.google.android.material.tabs.TabLayout
...
app:tabIndicatorColor="?colorPrimary"
/>
Use app:tabBackground
with selector
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With