Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom drawable as tabIndicator of com.google.android.material.tabs.TabLayout not working

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.

like image 985
Divyang Panchal Avatar asked Oct 14 '19 11:10

Divyang Panchal


People also ask

How to design tabLayout in Android?

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.

How to add tabs to tabLayout?

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.

How to display tabs in 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 ().

How to use tabs in Android?

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. ...

How to create Google tab layout in Android?

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.

Why is the indicator not showing in tablayout?

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.


3 Answers

I had the same issue when using a custom drawable. What fixed it for me was setting the tab indicator height.

app:tabIndicatorHeight="2dp"
like image 193
LeHaine Avatar answered Sep 23 '22 21:09

LeHaine


There is a separate attribute for the tab indicator color:

<com.google.android.material.tabs.TabLayout
    ...
    app:tabIndicatorColor="?colorPrimary"
/>
like image 20
Sandi Avatar answered Sep 23 '22 21:09

Sandi


Use app:tabBackground with selector

like image 26
Md. Asaduzzaman Avatar answered Sep 25 '22 21:09

Md. Asaduzzaman