3

I can not disable a tab in android tabbed activity. The tabbed activity have 3 tabs and I want to disable the tab in the middle.

I tried the following code in my Fragment but the variable middleTabView is always null!

TabLayout tabhostNew = (TabLayout) getActivity().findViewById(R.id.tabs);
TabLayout.Tab middleTabView  = tabhostNew.getTabAt(1).getCustomView();
middleTabView.setEnabled(false); //does not work, because middleTabView is null

The following code should work, but i am not able to get the variable tabwidget.

tabHost.getTabWidget().getChildTabViewAt(your_index).setEnabled(false);

Can you please help me? Thank you in advance!

A J
  • 3,970
  • 14
  • 38
  • 53
atas priv
  • 41
  • 3

1 Answers1

0

The method you are trying to call getTabWidget() is implemented in the TabHost class and not in TabLayout (which you are using).

Check out this answer:

TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager); // if you are using a view pager

LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
    tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
}
winklerrr
  • 13,026
  • 8
  • 71
  • 88