Tab Background change on selection
Question
I need to set different Images as tab background on different states. I have set one image as background for default but how to switch to other one when tab is selected. Below is my code.
public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
TabWidget tw = getTabWidget();
for (int i = 0; i < tw.getChildCount(); i++) {
View v = tw.getChildAt(i);
v.setBackgroundDrawable(getResources().getDrawable
(R.drawable.tab_artist));
}
//First tab
intent = new Intent().setClass(this, FirstActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("First").setIndicator("First")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabselected);
//Second tab
intent = new Intent().setClass(this, SecondActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Second").setIndicator("Second")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabselected);
//third
intent = new Intent().setClass(this, ThirdActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Third").setIndicator("Third")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tabselected);
}
}
/*tab_artist.xml*/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:background="@drawable/tabselected" android:state_selected="true" />
<!-- When not selected, use white-->
<item android:background="@drawable/tabunselected" />
</selector>
Solution
public class HelloTabWidget extends TabActivity implements OnTabChangeListener{
.....
mTabHost. setOnTabChangedListener(this);
@Override
public void onTabChanged(String tabId) {
// Here in tabId you will get the name of the Tab from that you can check and set the background
// of the requirement tab according to need.
}
}
OTHER TIPS
Implement onTabChangeListener()
and there modify their backgrounds. Cheers
http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html
Edit: Use the tabHost to implement the method. You can implemented where ever you want. Let's say do it after you set all the TabWidgets. Its good practice to use ids of the tab like you've set them "First", "Second" etc etc.
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
if(tabId.equals("First"){
//do something
}else if(tabId.equals("Second"))
{
//do something
}// etc etc etc
}});
this may help you
Tabhost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
for(int i=0;i<tb.getTabWidget().getChildCount();i++)
{
tb.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabunselected); //unselected
}
tb.getTabWidget().getChildAt(tb.getCurrentTab()).setBackgroundResource(R.drawable.tabselected);
}});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow