Search code examples
androidandroid-tablayout

Hide Tab in Tablayout Android


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>

Solution

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