Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent TabHost Tab Change?

Tags:

android

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.

like image 271
Fresh One Avatar asked Apr 13 '11 19:04

Fresh One


2 Answers

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;
            }               
        });
    }
like image 125
Urho Avatar answered Nov 08 '22 08:11

Urho


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);
}
like image 45
Minh Khoi Avatar answered Nov 08 '22 08:11

Minh Khoi