2

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()); } } 
4
  • You can't receive the click event when a View is disabled. Try with TouchListener instead. Here you can find an example how to detect click event in TouchListener. Commented Jan 8, 2016 at 13:04
  • stackoverflow.com/questions/4497187/… Commented Jan 8, 2016 at 13:06
  • i try now another way. please look the update part of my first post. i works fine. but the problem is, that i have to set the tab indicator back to tab 1. is there an way? Commented Jan 8, 2016 at 18:11
  • @SpecialFighter I'm getting tabLayout.getChildCount() always as 1 irrespective of how many Tabs I add. Any idea why? Commented Aug 13, 2018 at 17:41

2 Answers 2

6

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); } 
Sign up to request clarification or add additional context in comments.

Comments

0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.