Reputation: 335
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".
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
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
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
Reputation: 1658
This work for
((LinearLayout) Objects.requireNonNull(tabLayout.getTabAt(1)).view).setVisibility(View.INVISIBLE);
Upvotes: 0
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
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
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