Search code examples
androidandroid-tabhostandroid-image

How to change the Tabs Images in the TabHost


I am using the TabHost in my application, I am using four Tabs in my application and I want to use the different Images in the TabHost when the Particular Tab is been Selected and not selected. I need to use to different Images for a particular tab each.

When I Select any Tab the Image is little bright and when I switch to another Tab that bright Image becomes grey shaded.

I have implemented the TabHost but I don know how to change the Images in the TabHost.

Can anybody help me in this.

Thanks, david


Solution

  • If you wish to use different images for the selected and unselected states, then create 'selector' XML files in your drawables folder for each tab, e.g. tab1_selector.xml, tab2_selector.xml which should contain the following, replacing the drawable references to your images for selected and unselected states. i.e.

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

    Then using the .setIndicator method as bharath wrote above you should reference your new xml drawable resource.