7

I using the control android.support.design.widget.TabLayout

I have 4 tabs - on each tab i show some ViewPager - ( using fragment to show the different viewPager )

I want to disable all the tabs until the user will add some data that exist on the first tab.

I don't find any way to disable the tabs.

The code:

 <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/detailsElementBackground"
        android:clickable="true"
        app:tabGravity="center"
        app:tabMode="scrollable"
        app:tabTextAppearance="@style/MineCustomTabText" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.AppBarLayout>


 ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());


    adapter.addFragment(new Fragment1(), "Fragment1");
    adapter.addFragment(new Fragment2(), "Fragment2");
    adapter.addFragment(new Fragment3(), "Fragment3");
    adapter.addFragment(new Fragment4(), "Fragment4");

// need to disable Fragment2 & Fragment3 & Fragment4 until the user will add some string that exist on Fragment1

Yanshof
  • 9,659
  • 21
  • 95
  • 195

6 Answers6

3

Try this:

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

Reference: Disable TabLayout

Pang
  • 9,564
  • 146
  • 81
  • 122
akshay_shahane
  • 4,423
  • 2
  • 17
  • 30
3

there's my solution. I use Kotlin language, Java is the same

private fun disableTab(tabLayout: TabLayout, index: Int) {
    (tabLayout.getChildAt(0) as ViewGroup).getChildAt(index).isEnabled = false
}

It's worked for me!

2

If you want to handle the onTabSelected for your TabLayout, you can do this and check if they're allowed to show that Fragment.

tab_layout.addOnTabSelectedListener( new TabLayout.OnTabSelectedListener() {
            override onTabReselected( TabLayout.Tab tab ) {}
            override onTabUnselected( TabLayout.Tab tab ) {}
            override onTabSelected( TabLayout.Tab tab ) {
                if( ... is not disabled )
                pager.currentItem = tab.position
            }

        })
advice
  • 5,778
  • 10
  • 33
  • 60
2

Sounds like what you want to do is disable the ability to switch tabs to start with, then reenable it once some data has been entered. That's easy enough you just need to do two things:

Once you've implemented both the above you can disable switching tabs in your onCreate method with something like

tabs.isEnabled = false
viewpager.setSwipePagingEnabled(false)

once the user has entered the data on the first tab you can reenable it again

tabs.setEnabled(true)
viewpager.setSwipePagingEnabled(true)
Matt Smith
  • 836
  • 10
  • 21
1

If you want to disable tab, you just need use a customView

First at all, create your custom layout

v_tabview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tabItemView"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:maxLines="1"
  android:textColor="@drawable/selector_tab" />

create selector, for changing state enable/disable (changing color)

selector_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="#9e9e9e" android:state_enabled="false" /> //gray
  <item android:color="#64b246" android:state_enabled="true" /> //green
</selector>

then inflate it, set names and add to the tabLayout

arrayStringNames.forEach { name ->
    val textView: TextView = inflater.inflate(R.layout.v_tabview, tabLayout, false) as TextView
    textView.text = name
    val tab = tabLayout.newTab()
    tab.customView = textView
    tabLayout.addTab(tab)
}

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

 for (index in 0 until tabLayout.tabCount) {
   ((tabLayout.getTabAt(index)?.customView) as? TextView)?.let { textView ->
     textView.isEnabled = enable //boolean
     (textView.parent as View).enable(enable)
  }
}
gbixahue
  • 1,383
  • 10
  • 9
1

ViewPager2 + tabLayout

TabLayoutMediator(Tabs,ViewPager) { tab,  pos->
   tab.view.isEnabled = false
}.attach()
HUAN XIE
  • 175
  • 1
  • 3