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.
Can someone explain me how to fit the tabs to entire screen. Thanks for your time.
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
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With