Babez21584
Babez21584

Reputation: 93

Enable/Disable Tab in ActionBar

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??

Upvotes: 6

Views: 11608

Answers (6)

Morteza Rastgoo
Morteza Rastgoo

Reputation: 6986

This is done by two simple steps(Assuming you set custom view to the tabs):

1- Disable function: Avoid changing tabs

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        int position = tab.getPosition();
        if (mModifyMode) {
            mPagerAdapter.changeFragment(position);
        }
    }

2- Disable click events:

    public void setModifyMode(boolean modifyMode) {
        mModifyMode = modifyMode;
        for (int i = 0; i < mActionBar.getTabCount(); i++) {
            View parent = (View) mActionBar.getTabAt(i).getCustomView().getParent();
            parent.setEnabled(mModifyMode);
        }
    }

I tested it right now and it is working ;)

Upvotes: 0

tm1701
tm1701

Reputation: 7581

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.

Upvotes: 3

suckgamony
suckgamony

Reputation: 804

Late answer but hope this workaround might help others who are interested in this issue.

private Handler mHandler;
private int mLastSelectedTabIndex = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    mHandler = new Handler();

    // ...

    ActionBar bar = getActionBar();

    Tab tab = bar.newTab().setText("Enabled Tab")
            .setTabListener(mTabListener);
    bar.addTab(tab);

    tab = bar.newTab().setText("Disabled Tab")
            .setTabListener(mTabListener);
    bar.addTab(tab);
}

private TabListener mTabListener = new TabListener() {
    // ...

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        int position = tab.getPosition();
        if (position == 1) {
            // Disabled tabs are selected.

            mHandler.postAtFrontOfQueue(new Runnable() {
                @Override
                public void run() {
                    getActionBar().setSelectedNavigationItem(
                            mLastSelectedTabIndex);
                }
            });
        } else {
            // Enabled tabs are selected. Do something on your own.

            mLastSelectedTabIndex = position;
        }
    }

    // ...
};

If you are using ViewPager with it as usual, you can simply utilize its position instead of mLastSelectedTabIndex like this:

getActionBar().setSelectedNavigationItem(mViewPager.getCurrentItem());

Upvotes: 0

getmsg
getmsg

Reputation: 132

you can override: public boolean onPrepareOptionsMenu(Menu menu), in here set the tab enable/disable for example:

menu.findItem(R.id.send).setEnabled(mMessageNeedtoSend);

and then you can set the mMessageNeedtoSend true or false,call invalidateOptionsMenu() to refresh ActionBar.

Upvotes: 0

kaspermoerch
kaspermoerch

Reputation: 16570

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 :)

Upvotes: 4

kaspermoerch
kaspermoerch

Reputation: 16570

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 );

Upvotes: 7

Related Questions