Disable TabLayout Disable TabLayout android android

Disable TabLayout


I had the same problem and I solved it disabling touch event on tabs with the following code:

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


I found a similar answer that is a little more simple and also allows you to re-enable the tabs later if you wanted to without having to deal with overriding the onTouch event.

TabLayout tabLayout = (TabLayout)  mParentView.findViewById(R.id.my_tabs);LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));tabStrip.setEnabled(false);for(int i = 0; i < tabStrip.getChildCount(); i++) {    tabStrip.getChildAt(i).setClickable(false);}

And if you want to re-enable the tabs just set tabStrip.setEnabled and setClickable for the child elements to true

LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));tabStrip.setEnabled(true);for(int i = 0; i < tabStrip.getChildCount(); i++) {    tabStrip.getChildAt(i).setClickable(true);}


Very similar to the answer by pat8719 but just disabling the tabs is sufficient to prevent them from being selected.

TabLayout tabLayout = (TabLayout)  mParentView.findViewById(R.id.my_tabs);TabLayoutUtils.enableTabs( tabLayout, false );

TabLayoutUtils class

public class TabLayoutUtils {    public static void enableTabs(TabLayout tabLayout, Boolean enable){        ViewGroup viewGroup = getTabViewGroup(tabLayout);        if (viewGroup != null)            for (int childIndex = 0; childIndex < viewGroup.getChildCount(); childIndex++)            {                View tabView = viewGroup.getChildAt(childIndex);                if ( tabView != null)                    tabView.setEnabled(enable);            }    }    public static View getTabView(TabLayout tabLayout, int position){        View tabView = null;        ViewGroup viewGroup = getTabViewGroup(tabLayout);        if (viewGroup != null && viewGroup.getChildCount() > position)            tabView = viewGroup.getChildAt(position);        return tabView;    }    private static ViewGroup getTabViewGroup(TabLayout tabLayout){        ViewGroup viewGroup = null;        if (tabLayout != null && tabLayout.getChildCount() > 0 ) {            View view = tabLayout.getChildAt(0);            if (view != null && view instanceof ViewGroup)                viewGroup = (ViewGroup) view;        }        return viewGroup;    }}