0

I am trying to hide a tab dynamically using databinding. the code is

<com.google.android.material.tabs.TabItem
        android:id="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tab1" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/tab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tab2" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/tab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="@{vm.flag ? View.VISIBLE : View.GONE}"
        android:text="@string/tab3" />

but this is not working and all three tabs are always visible.

I tried setting it in the code as tablayout.getindex(2).visibility but this gives me NPE. How do I do this?

png
  • 4,368
  • 7
  • 69
  • 118

1 Answers1

0

You need to modify the adaterList that you pass to the viewpager. Delete the third element and it will disappear

When you need the third tab to appear, simply update the adapterList/viewPager. Check out this post

Mervin Hemaraju
  • 1,921
  • 2
  • 22
  • 71
  • I am not using adapter or viewpager. I have only 3 tabs and i want one of that to be dynamic. So I am adding the fragments to those positions – png Oct 09 '20 at 12:25