Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 257
Ina Bertolini Avatar asked Jul 08 '16 13:07

Ina Bertolini


1 Answers

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");
like image 192
C. Yujin Avatar answered Oct 11 '22 05:10

C. Yujin