tags:

views:

55

answers:

2

Hi all

i am using tab view in one of my activity. I want to change the drawable of the tabs on the basis of their selection. So it is like this -- i have 4 images T11, T12, T21, T22. I want to set the image T11 and T22 initially with tab 1 selected. Now i want to change the images to T12 and T21 as soon as i select Tab 2.

So far i tried using via an xml file of drawable type:

drawable for left tab( tab1) --

<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;

 <item android:state_window_focused="false" android:state_enabled="true"
  style="?attr/left_active" />

 <item android:state_window_focused="false" android:state_enabled="false"
  style="?attr/left_inactive" />
 <item android:state_pressed="true" android:state_enabled="true"
  style="?attr/left_active" />
 <item android:state_pressed="true" android:state_enabled="false"
  style="?attr/left_inactive" />
</selector>

Drawable for Tab right(Tab2) --

 <item android:state_window_focused="false" android:state_enabled="true"
  style="?attr/right_active" />

 <item android:state_window_focused="false" android:state_enabled="false"
  style="?attr/right_inactive" />
 <item android:state_pressed="true" android:state_enabled="true"
  style="?attr/right_active" />
 <item android:state_pressed="true" android:state_enabled="false"
  style="?attr/right_inactive" />

In activity:

TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1", getResources().getDrawable(R.drawable.left)).setContent(new Intent(this, Left.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
    .setIndicator("Tab2", getResources().getDrawable(R.drawable.right))
    .setContent(new Intent(this, Right.class)));
tabHost.setCurrentTab(1);

Please help...

+1  A: 

I've done this in my app, but not using xml/styles. I did it in code and then swap the background images in the onTabChanged() method.

Part of the code you can see in my comment in post http://stackoverflow.com/questions/3103062/android-tabhost-activities-within-each-tab/3103156#3103156

The onTabChanged would then look like this:

public void onTabChanged(String tabId) {
        if ("tabMap".equals(tabId)) {
            txtTabMap.setBackgroundDrawable(getResources().getDrawable(newsList==null?R.drawable.bg_tab_right_active_left_inactive:R.drawable.bg_tab_middle_active_both_inactive));
            txtTabInfo.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_left_inactive_right_active));
            if(txtTabNews!=null)txtTabNews.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_right_inactive_left_active));
        } else if ("tabInfo".equals(tabId)) {
            scrlDescription.scrollTo(0, 0);
            txtTabMap.setBackgroundDrawable(getResources().getDrawable(newsList==null?R.drawable.bg_tab_right_inactive_left_active:R.drawable.bg_tab_middle_inactive_left_active));
            txtTabInfo.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_left_active_right_inactive));
            if(txtTabNews!=null)txtTabNews.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_right_inactive_left_inactive));
        } else if ("tabNews".equals(tabId)) {
            txtTabMap.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_middle_inactive_right_active));
            txtTabInfo.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_left_inactive_right_inactive));
            if(txtTabNews!=null)txtTabNews.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_right_active_left_inactive));
        }
    }
Mathias Lin
Thanks Mathias, but i have another issue.. I am using themes in my application, so there will more than one image for a tab and will change if user changes the theme of the application. So how i do implement this??
mudit
A: 
mudit