Shaheen Zahedi Shaheen Zahedi - 1 month ago 9
Android Question

selectableItemBackground with white background on tablayout?

I had a tab layout

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:animateLayoutChanges="true"
android:background="@color/background_white" />


for implementing ripple animation I have to change the background to

android:background="?attr/selectableItemBackground"


It enables the ripple animation but the default color is kind of gray and I want my background to be a customized color like white, I also tried

android:background="@color/white"

app:tabBackground="?attr/selectableItemBackground"


but It doesn't appear when the background color is white,

I just don't know what's the reason that it doesn't work on white background?

Answer

finally, I find out how to get a background and selectable item together. first you need to declare two styles in your styles.xml,like below

    <style name="SelectableItemTheme">
    <item name="colorControlHighlight">@color/light_gray</item>
    </style>
    <style name="SelectableItemBackground">
    <item name="android:theme">@style/SelectableItemTheme</item>
    <item name="android:background">?attr/selectableItemBackground</item>
    </style>

and after that you assign it as an style parameter to tab-layout and put the color that you want from it,

                <android.support.design.widget.TabLayout
                   android:id="@+id/tabs"
                   android:layout_width="match_parent"
                   android:layout_height="60dp"
                   android:layout_alignParentStart="true"
                   style="@style/SelectableItemBackground"
                   android:background="@color/background_login"/>