Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change icon in Tablayout on selected and deselected state

Sorry if this question have been asked before.

I want to change icon when it is selected in tab of tab layout. How can I do this using selector?.

I have two tabs in my application on selected state icon should change.

like image 661
FaisalAhmed Avatar asked Jul 29 '16 05:07

FaisalAhmed


1 Answers

1. create a custom tab selector- You need to add both state selected true and false

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/home_fill"
            android:state_selected="true"/>
        <item android:drawable="@drawable/home_line"
            android:state_selected="false"/>
    </selector>

2. Add the custom tab selector drawable as the icon for tabItem

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/footer"
        app:tabIndicatorColor="@color/female_colour">

        <android.support.design.widget.TabItem
            android:id="@+id/tab_home"
            android:layout_width="@dimen/_25dp"
            android:layout_height="@dimen/_25dp"
            android:padding="@dimen/_4dp"
            android:icon="@drawable/tab_home"
            />
 </android.support.design.widget.TabLayout>

Code is Tested on Android version 7.1.1

like image 196
Raja chakraborty Avatar answered Sep 24 '22 13:09

Raja chakraborty