I have used TabLayout
from the latest design support library in my app. The tabs are attached to a viewpager which loads the fragments for each tab. I want to disable all the tabs until the viewpager loads the fragment for user selected tab. I am not able to disable the tablayout or make it non-clickable. I had used setEnabled(false)
and setClickable(false)
but it is not working. I am able to make it invisible by using setVisiblity(View.GONE)
but I want the tabs to be visible at all times.
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.near_me_hover).setTag(1));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.all_hostels).setTag(2));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.top_five).setTag(3));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.advanced_search).setTag(4));
tabLayout.setEnabled(false);
tabLayout.setClickable(false);
XML
android.support.design.widget.TabLayout
android:id="@+id/tabLayout" android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:scrollbars="horizontal"
android:splitMotionEvents="false" >
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
viewPager.setCurrentItem(tab.getPosition());
tab.setIcon(R.drawable.near_me_hover);
break;
case 1:
viewPager.setCurrentItem(tab.getPosition());
tab.setIcon(R.drawable.all_hostels_hover);
break;
case 2:
viewPager.setCurrentItem(tab.getPosition());
tab.setIcon(R.drawable.top_five_hover);
break;
case 3:
viewPager.setCurrentItem(tab.getPosition());
tab.setIcon(R.drawable.advanced_search_hover);
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
tab.setIcon(R.drawable.near_me);
break;
case 1:
tab.setIcon(R.drawable.all_hostels);
break;
case 2:
tab.setIcon(R.drawable.top_five);
break;
case 3:
tab.setIcon(R.drawable.advanced_search);
break;
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tabLayout.getTabAt(position).select();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
If you want to disable tab, you just need use a customView
First at all, create your custom layout (textView as an example)
v_tabview.xml
create selector, for changing state enable/disable (changing color)
selector_tab.xml
then inflate it, set names and add to the tabLayout
and at the end, a magic trick! In that sample code I disabling all tabs. If you need disable second and third tab, check "index" in a cycle and disable if you need
Another trick:
You can put another blank transparent view upon tablayout until your requirement fulfill. When you need to enable/show the tabs then just hide the blank view.
you can create a util function, my fun in Kotlin:
When you want do something with a view, you can debug or click in parrent view to know how it's created, by this way you can do anything you that you want. For this case, you can go to Tablayout class to understand.
If you mean to disable one tab button on TabLayout, then try this code:
there are 3 methods implemented by the tab click listener, one of them is onTabSelected() put a boolean condition to check if your fragment is initialised. Then if that condition is satisfied then allow transaction to take place. Also initialize the tabs after your fragment code