Enable/Disable Tab in ActionBar

11,564

Solution 1

You could use the removeTab( ActionBar.Tab tab )-method of ActionBar:

bar.removeTab( tab );

And then use the addTab( ActionBar.Tab tab, int position ) to put it back in, provided that you save the position of the Tab you removed:

bar.addTab( tab, savedTabPosition );

Solution 2

I haven't tested this - that will be up to you, but it should give you an general idea on how you could handle your problem.

There are three steps:

First step

We need something that can handle the enable/disable action for us. For this purpose we create the following class:

public class TabItem {
  private Tab tab;
  private Fragment fragment;
  private boolean enabled;

  public TabItem( Tab tab, Fragment fragment ) {
    this.tab = tab;
    this.fragment = fragment;
    enabled = true;
  }

  public Tab getTab() {
    return tab;
  }

  public Fragment getFragment() {
    return fragment;
  }

  public void toggleEnabled() {
    enabled = enabled ? false : true;
  }

  public boolean isEnabled() {
    return enabled;
  }
} 

Second step

We need something that can hold these TabItems and an easy way to access them. For this purpose we add the following class:

public class TabHolder {
  private HashMap<Integer, TabItem> tabs;

  public TabHolder() {
    tabs = new HashMap<Integer, TabItem>();
  }

  public void addTab( TabItem tab ) {
    tabs.put( tab.getTab().getPosition(), tab );
  }

  public TabItem getTab( int position ) {
    return tabs.get( position );
  }
}

Third step

We need to handle the selection of Tabs ourselves, so we need to create a custom TabListener:

private class MyTabListener implements TabListener {
  @Override
  public void onTabReselected( Tab tab, FragmentTransaction ft ) {
    //Do nothing - unless you want to do something.
  }

  @Override
  public void onTabSelected( Tab tab, FragmentTransaction ft ) {
    TabItem item = tabHolder.getTab( tab.getPosition() );
    if( item.isEnabled() ) {
      ft.remove( item.getFragment() );
      ft.commit();
    }        
  }

  @Override
  public void onTabUnselected( Tab tab, FragmentTransaction ft ) {
    //Do nothing - unless you want to do something.        
  } 
}

Finally

We can now utilize our created framework. To do so, we need a TabHolder:

tabHolder = new TabHolder(); //Needs to be declared in the same class as our TabListener

We need to add our Tabs to this:

tabHolder.addTab( new TabItem( tab, fragmentForThisTab ) );

And we need to set our custom TabListener on each Tab:

tab.setTabListener( new MyTabListener() );

Enable/Disable

To enable or disable a Tab we simply call:

tabHolder.getTab( position ).toggleEnabled();

Let me know how it goes :)

Solution 3

There is a simple way to remove the Tabs-bar from the Actionbar. Just type:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

This will remove any tabs-bar.

Share:
11,564
Babez21584
Author by

Babez21584

Updated on June 09, 2022

Comments

  • Babez21584
    Babez21584 almost 2 years

    It's possible to enable/disable Tabs in ActionBar? With TabHost this is not a problem.. I do:

    tabHost.getTabWidget().getChildAt(3).setVisibility(true);
    

    and all works.. but if i want to do the same thing with Tabs in ActionBar?? In Tab class don't exist setEnable();

    ActionBar bar = getActionBar();
    Tab tab =  bar.newTab();
    tab.setText("Test");
    tab.setEnable(false); /*DON'T EXIST!!*/
    

    How can I do??