androidandroid-tablayoutshapesripple

How can I set round ripple shape of TabLayout's ta?


I'm going to set round ripple shape of tablayout's tab. The TabLayout is linked to viewpager. I tried to define selector XML and set tabbackground attribute of tablayout with the selector xml. So it has been set round. But the issue is that it works after I click it first. So that means the ripple of every tab doesn't work at the first click. What's the reason? Thanks.

This is my code.

<com.google.android.material.tabs.TabLayout
        android:id="@+id/unit_categories"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabBackground="@drawable/unit_category_selector_tab_background"
        app:tabGravity="center"
        app:tabIndicator="@drawable/unit_category_tab_indicator"
        app:tabIndicatorFullWidth="true"
        app:tabIndicatorGravity="center"
        app:tabIndicatorHeight="40dp"
        app:tabMode="scrollable"
        app:tabRippleColor="@android:color/transparent"
        app:tabUnboundedRipple="false" />

This is unit_category_selector_tab_background.xml

<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true" android:state_selected="false" android:drawable="@drawable/unit_category_tab_ripple"/>
</selector>

This is unit_category_tab_ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#9BB3B1">
    <item>
        <shape
            android:shape="rectangle"
            >
            <solid android:color="#FFFEFE" />
            <corners
                android:radius="40dp" />
        </shape>
    </item>
</ripple>

Solution

  • <com.google.android.material.tabs.TabLayout
    android:id="@+id/unit_categories"
    android:layout_width="match_parent"
    android:layout_height="@dimen/unit_category_tab_height"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:background="@android:color/transparent"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    app:tabGravity="center"
    app:tabIndicator="@drawable/unit_category_tab_indicator"
    app:tabIndicatorFullWidth="true"
    app:tabIndicatorGravity="center"
    app:tabIndicatorHeight="@dimen/unit_category_tab_height"
    app:tabMode="scrollable"
    app:tabRippleColor="@android:color/transparent"
    app:tabTextColor="@color/unit_calc_text_color"
    app:tabUnboundedRipple="false" />
    

    tab_indicator.xml

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#D584D5">
    <item>
        <shape
            android:shape="rectangle">
            <!-- radius should be half of the desired TabLayout height -->
            <corners
                android:radius="@dimen/unit_category_tab_height"/>
            <!-- color of the selected tab -->
            <solid android:color="@android:color/transparent"/>
            <stroke android:width="1dp" android:color="@color/tab_indicator_color"/>
        </shape>
    </item>
    </ripple>
    

    tab_background.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="0" android:exitFadeDuration="1500">
        <item android:state_pressed="true" android:state_selected="false" android:drawable="@drawable/unit_category_tab_ripple"/>
    </selector>
    

    I set background by the code when the tab changes

    tab_riple.xml

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#C1DCD9">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/calculator_primary_color" />
                <corners android:radius="@dimen/unit_category_tab_height" />
                <stroke android:width="2dp" android:color="@android:color/transparent"/>
            </shape>
        </item>
    </ripple>