Search code examples
androidandroid-xmlandroid-tabs

Android how to highlight drawable of selected action bar tab


I am looking to add action bar tabs with icons. I have achieved the following.

enter image description here

In this, how do I make the selected tab icon orange i.e. if the first tab is selected, the first icon is orange, while the others stay gray and so on. Thanks


Solution

  • Possible solution is. You must create an icon in white and orange and put it in your drawables folder and in the onCreate method of your currently tab add this here:

    //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 hope this helps.