Hide Tab in Tablayout Android
Asked Answered
H

7

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>
Heresy answered 8/7, 2016 at 13:25 Comment(5)
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.Quinnquinol
please, share the source codeCommoner
Can't suggest anything without the applicable codeAnson
please, share also the source code that adds the tabs to the TabLayoutCommoner
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 adapterHeresy
E
21

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

Earthlight answered 25/4, 2019 at 4:39 Comment(0)
M
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(); 
Merge answered 6/7, 2017 at 3:14 Comment(2)
this is not the right way to do it and it doesn't workPembroke
It works in my case. Thanks. Just to handle null pointer exception here.Ternan
C
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

Commoner answered 8/7, 2016 at 14:21 Comment(2)
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.Heresy
when the third tab should appear, simply update adapterList/viewPager. You can have some ides studying thisCommoner
P
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));
Popup answered 3/11, 2018 at 14:4 Comment(0)
H
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
Hager answered 23/6, 2021 at 13:37 Comment(1)
this is way to go.Anjanette
S
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");
Sediment answered 21/10, 2020 at 6:59 Comment(0)
P
0

This work for

 ((LinearLayout) Objects.requireNonNull(tabLayout.getTabAt(1)).view).setVisibility(View.INVISIBLE);
Pipsqueak answered 20/6, 2020 at 3:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.