Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab with icon using TabLayout in Android Design Library

I'm trying to use the new TabLayout in the android design library to create app bar with icons only.

like this: enter image description here

how can I do it using the new TabLayout Android Design Library.

is there a simple solution for this, or i have to use the setCustomView only. i'm trying to avoid using it. because i didn't get the tint color for the icon like this image above.

i try to write like this:

tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_dashboard))

but the icon still stay in the same color when i select the tab

like image 668
Tzahie Leibovich Avatar asked Jun 14 '15 11:06

Tzahie Leibovich


2 Answers

you have to create a selector for the icon. For example:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_dashboard_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/ic_dashboard_selected"
          android:state_selected="true" />
    <item android:drawable="@drawable/ic_dashboard_normal" />
</selector>
like image 66
Budius Avatar answered Nov 08 '22 17:11

Budius


I used it like this: created an xml file in drawable as shown by @Budius.

in the code: tabLayout.getTabAt(0).setIcon(R.drawable.settings_tab_drawable);

and so on.

like image 21
Nabeel K Avatar answered Nov 08 '22 17:11

Nabeel K