Ina Bertolini
Ina Bertolini

Reputation: 335

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>

Upvotes: 16

Views: 21831

Answers (7)

Ahmet B.
Ahmet B.

Reputation: 1674

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

Upvotes: 2

C. Yujin
C. Yujin

Reputation: 11

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

Upvotes: 1

msayubi76
msayubi76

Reputation: 1658

This work for

 ((LinearLayout) Objects.requireNonNull(tabLayout.getTabAt(1)).view).setVisibility(View.INVISIBLE);

Upvotes: 0

aLL
aLL

Reputation: 1726

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

Upvotes: 22

Nowrose Muhammad Ragib
Nowrose Muhammad Ragib

Reputation: 615

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

Upvotes: 4

prakash selvam
prakash selvam

Reputation: 319

 //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(); 

Upvotes: 9

ddb
ddb

Reputation: 2435

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

Upvotes: 4

Related Questions