2

I have a tabhost with icon, when a tab is selected X, the icon does not appear because the icon is the same color as the selected tab. The question is:

How do I change the icon, when a tab is selected X?

Pedro
  • 451
  • 2
  • 6
  • 5

1 Answers1

16

This is what I have:

//TabActivity.onCreate()
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;

intent = new Intent().setClass(this, YourClass.class);
spec = tabHost.newTabSpec("tab_name").setIndicator("Tab Text",
            getResources().getDrawable(R.drawable.ic_tab_dialer))
            .setContent(intent);
tabHost.addTab(spec);

Then, you need to add ic_tab_dialer.xml to res/drawable/ directory with this content:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/ic_tab_selected_dialer" />
    <item android:drawable="@drawable/ic_tab_unselected_dialer" />
</selector>

I downloaded the icons from Contacts app GIT repo.:

git://android.git.kernel.org/platform/packages/apps/Contacts.git

licorna
  • 5,670
  • 6
  • 28
  • 39
  • 1
    Excellent! thank you so much. Just so people know, if you have tabs that do NOT lead to another activity upon clicking, you don't need the intent, the most important part is the `getResources().getDrawable(R.drawable.your_xml_file);` I had no text in my tab, nor did I use an Intent and this works like a charm. – Azurespot May 06 '14 at 05:32
  • Good solution for me. I'm using a a TabLayout just have to set the icon with the correct drawable something like : tab1.setIcon(getResources().getDrawable(R.drawable.ic_tab_one, getTheme())); – Alex DG Jan 18 '16 at 10:21