Reputation: 613
in my android app, i set an tab layout like this:
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("TAB 1"));
tabLayout.addTab(tabLayout.newTab().setText("TAB 2"));
tabLayout.addTab(tabLayout.newTab().setText("TAB 3"));
i would like to disable tab 2 and tab 3. i try this and it works:
tabLayout.getChildAt(1).setEnabled(false);
tabLayout.getChildAt(2).setEnabled(false);
but i also would like to set an toast feedback, if somebody tap on a disable tab, like "This Tab is not unlocked"
for this i try to set an onclicklistener for tab 2 and 3. but this listener doesn't work, if I disable the tab with the code before.
have anybody an idea how i can solve this problem?
UPDATE
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == 1) {
viewPager.setCurrentItem(0);
TabLayout.Tab tab1 = tabLayout.getTabAt(0);
tab1.select();
} else {
viewPager.setCurrentItem(tab.getPosition());
}
}
Upvotes: 2
Views: 5577
Reputation: 447
My TabLayout looks like the following
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab1" />
<android.support.design.widget.TabItem
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab2" />
<android.support.design.widget.TabItem
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab3" />
</android.support.design.widget.TabLayout>
You must cast the tabs you want to disable as view objects. Like this
View tab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(desiredPosition);
then you can change the clickability and set the alpha so that the tabs you want visually and functionally are enable or disable. in this way
ViewGroup tabItem = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(desiredPosition);
if (isChecked) {
tabItem.setClickable(false);
tabItem.setAlpha( 0.3F);
}else {
tabItem.setClickable(true);
tabItem.setAlpha( 1F);
}
Upvotes: 6
Reputation: 1008
I don't know that it is possible using disable and enable tab view but i have another way.
While user will click on the tab set flag like
if(enable != true)
// display toast
else
// enable event
Upvotes: 0