Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBar: show/hide tabs dynamically?

Is it possible to remove/restore the tab bar from the action bar dynamically?

Up to now I did this by changing the navigation mode of the action bar. I used following code to remove and restore the tab bar:

@Override 
public void restoreTabs() {     
    getSupportActionBar()
    .setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    this.supportInvalidateOptionsMenu();
}

@Override
public void removeTabs() {      
    getSupportActionBar()
    .setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    this.supportInvalidateOptionsMenu();
}

That works, but there is a big problem: Everytime I call setNavigationMode, onTabSelected is called in the TabListener and the currently opend tab gets recreated.

like image 810
Apfelsaft Avatar asked Apr 30 '13 11:04

Apfelsaft


1 Answers

To remove the actionbar tabs dynamically, you simply need:

getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

To add them on the fly, simply do:

getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

For the second case, the assumption is that after setting the navigation mode, you will also add tabs, to the action bar, similar to this:

for (int resourceId : tabs) {
        actionBar.addTab(actionBar.newTab().setText(resourceId)
                .setTabListener(this));
}
like image 187
Tash Pemhiwa Avatar answered Sep 23 '22 04:09

Tash Pemhiwa