Categories
android

SlidingTabLayout to fit the screen

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.

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