Is there any way with the stock Android TabHost to prevent a tab change from taking place ???
In iOS, there is a delegate callback called shouldSelectViewController on the tabBarController, which if you return FALSE prevents the tab change from taking place.
Android has an onTabChanged() delegate, but that appears to be an after-the-fact notification that the tab change has taken place (it returns void).
Thanks.
If you don't want to use onTabChanged() for that, you can set OnClickListener/OnTouchListener for each tab and do it there. Example:
for(int i=0; i<tabWidget.getTabCount(); i++) {
tabWidget.getChildAt(i).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
String currentTabTag = (String) tabHost.getCurrentTabTag();
String clickedTabTag = (String) v.getTag();
if(clickedTabTag.equals("BAD TAG")) {
return true; // Prevents from clicking
}
}
return false;
}
});
}
If you want to prevent the tab from changing, for example you always want the tab be tab 0, try this code:
if (tabId.equals("Tab 1")) {
// This will switch tab 1 to tab 0 immediately so that it seems like tab 0 does not change
getTabHost().setCurrentTab(0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With