Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a fixed number of tabs at a time in Android Material Design tabs?

I have used the Android Design Support library and implemented tabs in my android app using tablayout, fragments and viewpager. Tab sliding is working perfectly. But when I add more tabs (say 7), those tabs reduce their width and adjust themselves. How to show only 3 tabs at a time and show the next three when the user slides from 3rd to 4th tab?

like image 624
Vikas D Avatar asked Jan 08 '16 04:01

Vikas D


People also ask

What is TabLayout?

TabLayout is used to implement horizontal tabs. TabLayout is released by Android after the deprecation of ActionBar. TabListener (API level 21). TabLayout is introduced in design support library to implement tabs. Tabs are created using newTab() method of TabLayout class.

What is the use of tab bar in Android?

It provides a horizontal layout to display tabs on the screen. We can display more screens on a single screen using tabs. We can quickly swipe between the tabs.


1 Answers

In your MainActivity

ViewPager mpager;

TabPageAdapter adapter = new TabPageAdapter(getSupportFragmentManager(), list);

mtabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
mpager.setOffscreenPageLimit(list.size());

mpager.setAdapter(adapter);

In your xml TabLayout set

app:tabMode="scrollable" 
like image 129
Pitty Avatar answered Sep 30 '22 17:09

Pitty