Harry T.
Harry T.

Reputation: 3527

Hide TabLayout.Tab which integrated with ViewPager

How to hide a tab in TabLayout?

My TabLayout integrated with ViewPager so I cannot use TabItem for each Tab.

I was init my TabLayout and ViewPager like this. Thanks

    tabTitles = new ArrayList<>();
    tabTitles.add("Tab   1");
    tabTitles.add("Tab   2"); // I want to hide this tab and set visible later.
    tabTitles.add("Tab      3");
    tabTitles.add("Tab   4");
    tabTitles.add("Tab  5");

    adapter = new ProfileBirefAdapter(getSupportFragmentManager(), this, tabTitles);

    vpgMain.setAdapter(adapter);

    tabLayout.setupWithViewPager(vpgMain);

Upvotes: 0

Views: 1367

Answers (1)

Barak
Barak

Reputation: 1429

I'm not sure you can "hide" a tab, but you can remove & add it back again, as it will look like you've hidden it.


Step 1 - "Hide"

Remove your desired tab from the ArrayList:

tabTitles.remove(2);

Notify your ViewPager's Adapter so the update will take place

vpgMain.getAdapter().notifyDataSetChanged();

Step 2 - "Show"

Add your tab back again
(Note: to put it back in the same position, use this guy's answer to manipulate your ArrayList)

tabTitles.add("Tab   2");

And notify your adapter again:

vpgMain.getAdapter().notifyDataSetChanged();

Good luck!

Upvotes: 1

Related Questions