public class MyTabs extends TabActivity {
public static final String TAG_TAB1 = "TAB1";
public static final String TAG_TAB2 = "TAB2";
public static final int ID_TAB1 = 0;
public static final int ID_TAB2 = 1;
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec(TAG_TAB1)
.setIndicator(TAG_TAB1, getResources().getDrawable(R.drawable.graphictabred))
.setContent(new Intent(this, MyActivity_1.class))
);
setText(ID_TAB1, null, new Float(10.0));
tabHost.addTab(tabHost.newTabSpec(TAG_TAB2)
.setIndicator(TAG_TAB2, getResources().getDrawable(R.drawable.graphictabwhite))
.setContent(new Intent(this, MyActivity_2.class))
);
setText(ID_TAB2, null, new Float(10.0));
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
if(tabId.equals(TAG_TAB2)){
setIcon(tabHost.getCurrentTab(), R.drawable.graphictabred);
setText(tabHost.getCurrentTab(), tabId + " is selected", new Float(10.0));
setIcon(ID_TAB1, R.drawable.graphictabwhite);
setText(ID_TAB1, TAG_TAB1, new Float(10.0));
}
else if(tabId.equals(TAG_TAB1)){
setIcon(tabHost.getCurrentTab(), R.drawable.graphictabred);
setText(tabHost.getCurrentTab(), tabId + " is selected", new Float(10.0));
setIcon(ID_TAB2, R.drawable.graphictabwhite);
setText(ID_TAB2, TAG_TAB2, new Float(10.0));
}
}
});
}
private void setIcon(int whichTab, int resourceId){
ArrayList <View> views = tabHost.getTabWidget().getChildAt(whichTab).getTouchables();
RelativeLayout relLayout = (RelativeLayout)views.get(0);
ImageView iv = (ImageView)relLayout.getChildAt(0);
iv.setImageDrawable(getResources().getDrawable(resourceId));
}
private void setText(int whichTab, String text, Float size){
ArrayList<View> views = tabHost.getTabWidget().getChildAt(whichTab).getTouchables();
RelativeLayout relLayout = (RelativeLayout)views.get(0);
TextView tv = (TextView)relLayout.getChildAt(1);
if(text != null){
tv.setText(text);
}
tv.setTextSize(size);
}
}