DaniloBertelli
DaniloBertelli

Reputation: 121

Enable/Disable Android ActionBar.Tab

I'm developing an application that uses the new Tabs system (ActionBar.addTab()), I will have 3 tabs. During a determined process I would like to lock (disable) two of them (not remove, only do not accept clicks and not change the 'selector'.

I was able to do not change the content (by implementing the TabListener and not changing the fragment, etc) but the selector changes.

Is there anyway to only disable/enable the tabs? without have to remove and add again?

Thanks in advance! BR, Danilo

Upvotes: 8

Views: 3409

Answers (3)

Suhas Patil
Suhas Patil

Reputation: 131

I also faced same problem.My application has three tabs and each tab has buttons to move onto next and previous tab. I don't find any method for enabling or disabling tab.

I used DataSetter class to hold previously selected tab position and boolean value to determine tab selection. If boolean flag is true then user has pressed next/previous button. Otherwise user is trying to select tab by pressing them which we don't allow.
I used handler for tab selection.Inside fragment when user clicks next button I broadcast that request. Broadcast request has integer parameter which is position of next or previous tab that we want to set. Code inside fragment is:

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {
      View rootView = inflater.inflate(R.layout.first_tab_fragment, container, false); 
      next = (Button)rootView.findViewById(R.id.nextbutton);
      next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("TAB_CLICKED");
            Bundle bundle = new Bundle();
            bundle.putInt(MainActivity.POSITION,1);
            intent.putExtras(bundle);
            getActivity().sendBroadcast(intent);
        }
     });
    return rootView;
    }

I have used CustomViewPager instead of ViewPager to prevent tab swiping. CustomViewPager class is :

            public class CustomViewPager extends ViewPager {            

            private boolean enabled;            
            public CustomViewPager(Context context) {
                super(context);
            }

            public CustomViewPager(Context context, AttributeSet attrs) {
                super(context, attrs);
            }

            @Override
            public boolean onTouchEvent(MotionEvent event) {
                // TODO Auto-generated method stub
                if (this.enabled) {
                    return super.onTouchEvent(event);
                }

                return false;
            }

            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                // TODO Auto-generated method stub
                 if (this.enabled) {
                        return super.onInterceptTouchEvent(event);
                    }

                    return false;
            }

            public void setPagingEnabled(boolean enabled) {
                this.enabled = enabled;
            }             
        }

Inside oncreate method of MainActivity.java add this two lines:

     viewPager = (CustomViewPager) findViewById(R.id.pager);
     viewPager.setPagingEnabled(false);

Below is remaining code from MainActivity.java:

  @Override
  public void onTabSelected(Tab tab, FragmentTransaction ft) {

    Log.d("MainActivity","onTabSelected");  
    if(setter.getFlag() == true)
    {
        viewPager.setCurrentItem(setter.getposition());
    }           
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    Log.d("MainActivity","onTabUnselected");    
    //handler.sendEmptyMessage(0);
    Message message = new Message();
    message.arg1 = setter.getposition();
    handler.sendMessage(message);       
}

Handler handler = new Handler()
{
    public void handleMessage(android.os.Message msg)
    {
        int arg1 = msg.arg1;            
        Log.d("arg1",arg1+"");
        //viewPager.setCurrentItem(arg1);
        mActionBar.setSelectedNavigationItem(arg1);
        Log.d("handler","Inside handler");
    }
};

BroadcastReceiver receiver =  new BroadcastReceiver(){

    public void onReceive(Context context, android.content.Intent intent) {

        String action = intent.getAction();
        Bundle bundle = intent.getExtras();
        int position = bundle.getInt(MainActivity.POSITION);
        Log.d("Action",action);
        Log.d("position",position+"");
        setter.setposition(position);
        setter.setflag(true);
        Message message = new Message();
        message.arg1 = position;
        handler.sendMessage(message);
    };
   };
 }

Finally DataSetter.java :

    public class DataSetter 
    {
        int position;
        boolean flag;   
        void setposition(int value)
        {
            position = value;
        }   
        void setflag(boolean value)
        {
            flag = value;
        }   
        int getposition()
        {
            return position;
        }   
        boolean getFlag()
        {
            return flag;
        }
    }

More you can find on: http://suhas1989.wordpress.com/2014/10/13/enable-or-disable-actionbar-tab-in-android/

Upvotes: 0

KurtCobain
KurtCobain

Reputation: 631

I got into the exact same issue. Finally was able to solve it. As said already, there is no way to disable/enable a tab. There is only one way out - and that is to go back to the previous tab using

getActionBar().setSelectedNavigationItem(tab);

Note however, that this should NOT be done from within the

onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction)

method, because then it gets stuck in recursive loop. So within the onTabSelected() - send a messsage to the Activity Handler to do the setSelectedNavigation().

One catch here is (you will know when you implement): what "tab" value to pass in the setSelectedNavigation. For this, we have to keep track of whether it is a user pressed tab switch or forced switch to the last tab via the code.

For this:

public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    if (forcedTabSwitch)
        forcedTabSwitch = false;
    else 
        mTabToBeSwitchedTo = tab.getPosition();
}

and in the Handler,

switch (msg.what) {
            case SWITCH_TO_TAB :
                forcedTabSwitch = true;
                            break;
}

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007554

Sorry, there is no "enable" or "disable" with tabs in the action bar. As you note, you can remove and re-add them -- that is the closest you can get AFAIK.

Upvotes: 2

Related Questions