15

Unfortunately the other question wasn't answered about how to hide a Tab in android.support.design.widget.TabLayout. The others questions are made with TabHost, I don't want to change my code.

I would like to hide the tab "Three".

enter image description here


Fragment:

viewPager = (ViewPager) rootView.findViewById(R.id.search_viewPager);
viewPager.addOnPageChangeListener(viewPagerListener);
viewPager.setAdapter(adapter);
tabLayout = (TabLayout) rootView.findViewById(R.id.search_tabs);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">

    <android.support.design.widget.TabLayout
    android:id="@+id/search_tabs"
    style="@style/TabLayoutStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:elevation="1dp" />

    <android.support.v4.view.ViewPager
    android:id="@+id/search_viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" />

</LinearLayout>
Community
  • 1
  • 1
Ina Bertolini
  • 325
  • 1
  • 3
  • 11
  • 1
    Can you link the other question you looked at and explain why that solution didn't work for you? This is either a duplicate question, or it needs to be tweaked with more explanation. – AdamMc331 Jul 08 '16 at 13:27
  • 1
    please, share the source code – ddb Jul 08 '16 at 13:32
  • 1
    Can't suggest anything without the applicable code – BR89 Jul 08 '16 at 13:35
  • 1
    please, share also the source code that adds the tabs to the TabLayout – ddb Jul 08 '16 at 13:49
  • Hi the viewpager has a adapter (List) with my elements, so when i add the adapter into the viewPager and the viewPager into the tab the tab automatic get the same number of tabs as elements in my adapter – Ina Bertolini Jul 08 '16 at 13:54

7 Answers7

20

You can access the TabView of a TabLayout through tablayout.getTabAt(int index) and cast it as LinearLayout so you can set it's visibility to GONE

((LinearLayout) tabLayout.getTabAt(0).view).setVisibility(View.GONE);

aLL
  • 1,596
  • 3
  • 17
  • 30
9
 //To hide the first tab 
 ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
 //Set the next  tab as selected tab
 TabLayout.Tab tab = tabLayout.getTabAt(1);
 tab.select(); 
prakash selvam
  • 319
  • 4
  • 13
4

modify the adaterList you pass to the viewpager: delete the third element "Three", so it will disappear

EDIT

when the third tab should appear, simply update adapterList/viewPager. You can have some ideas studying this

Community
  • 1
  • 1
ddb
  • 2,423
  • 7
  • 28
  • 38
  • 1
    Sorry that is not exactly what i want the search has 3 differents types of search 2 of they are in the application the three one should only appear when the other 2 does not show a result. Like a web search. I really would like to hide this option as longer as in my app the other 2 options can be found. So these was the reason that i would like to hide and not to delete the tab. – Ina Bertolini Jul 08 '16 at 14:26
  • 1
    when the third tab should appear, simply update adapterList/viewPager. You can have some ides studying [this](http://stackoverflow.com/questions/10849552/update-viewpager-dynamically) – ddb Jul 08 '16 at 14:29
4

If you want to save the previous view(if you used any custom view inside tab item):

View view=tab_layout.getTabAt(3).getCustomView();

Then to remove the tab at index idx(say index 2):

tab_layout.removeTabAt(idx);

Then to restore that tab, use:

tab_layout.addTab(tab_layout.newTab().setCustomView(view));
2

In Kotlin you can hide Tab after defining TabLayoutMediator as below

TabLayoutMediator(
        tabLayout,
        viewPager
    ) { tab, position ->
        tab.text = when (position) {
            // define tab titles
        }
    }.attach()    

((tabLayout.getTabAt(position)?.view))?.isVisible = false
Ahmet B.
  • 1,290
  • 10
  • 20
1

I suggest you to use code below

var tabLayout = FindViewById<TabLayout>(YOUR_TAB_LAYOUT_ID);
tabLayout.SetupWithViewPager(YOUR_CUSTOM_VIEWPAGER);

**tabLayout.GetTabAt(0).View.Visibility = ViewStates.Gone;**

For making it happen, just insert dummy tab to index 0. You can do it in your custom viewpager adapter. Here is code.

adapter.addFragment(Fragmetn.NewInstance(), "none");
C. Yujin
  • 11
  • 1
0

This work for

 ((LinearLayout) Objects.requireNonNull(tabLayout.getTabAt(1)).view).setVisibility(View.INVISIBLE);
msayubi76
  • 1,446
  • 1
  • 12
  • 18