Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SlidingTabLayout to fit the screen

Tags:

android

I am using Google's SlidingTabLayout. As i only have 3 tabs to be displayed, I am seeing empty space after last tab. Please screenshot below. enter image description here

Can someone explain me how to fit the tabs to entire screen. Thanks for your time.

like image 571
Raju Avatar asked Nov 07 '14 14:11

Raju


3 Answers

Use this before setViewPager():

slidingTabs.setDistributeEvenly(true);

If you got this error Cannot resolve method 'setDistributeEvenly(boolean)', you should update your SlidingTabLayout.java and SlidingTabStrip.java from Google I/O source code:

SlidingTabLayout.java

SlidingTabStrip.java

like image 74
iForests Avatar answered Nov 13 '22 12:11

iForests


In SlidingTabLayout.java, find

protected TextView createDefaultTabView(Context context)

and add these lines to that method:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
textView.setWidth(size.x / 3);

That fixed it for me. Basically, you're getting the width of your screen and dividing by 3 (the number of tabs) and setting that as the width for the TextField that makes up your tab.

UPDATE:

In the current source, google added a setDistributeEvenly(Boolean); method, so you can use slidingTabs.setDistributeEvenly(true); instead. When using the older source code, either use my solution above, or update to the newest source (latter is recommended).

like image 44
SubliemeSiem Avatar answered Nov 13 '22 11:11

SubliemeSiem


Changing the SlidingTabLayout.java file may help. However, the solution may not seem very much generic. In the SlidingTabLayout.java file, search for the method

protected TextView createDefaultTabView(Context context)

Underneath the TextView 'set' methods, type in the following code.

textView.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));

Each tab strip is merely a customized LinearLayout.

like image 10
Achilles Rasquinha Avatar answered Nov 13 '22 11:11

Achilles Rasquinha