Setting width of tabs in android.support.design.widget.TabLayout? - android

I am using tabs for my Viewpager, and this is the XML right now:
<android.support.design.widget.TabLayout
android:id="#+id/viewpager_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"/>
They look great but I want to be able to have more tabs and not have everything squish on the screen. However I don't like switching to scrollable tabMode because then everything flattens out and looks messy. How can I set the width of the individual tabs?

TabLayout does not provide the attributes for particular tab's fixed width.
But you can set min and max width.
tabMinWidth and tabMaxWidth
https://developer.android.com/reference/android/support/design/widget/TabLayout.html

Related

MaterialViewPager full tab text without truncating it

I am working on an android library called MaterialViewPager
I have five tabs, the text of some of them are little longer. I purposely want to show full text in tabs (finance related application). Right now the page titles are getting truncated and replaced with ... (e.g. Exemption is being replaced with Exe...).
I could not find any setting that forces tab to be of full text/width.
Has anyone used this library before have achieved it? If not, is there any alternative library that can help me achieve it?
To Force tab to show Full text try to set the TabLayout property tabMode to scrollabale. TabMode is having two values
1. fixed - 1
2. scrollable - 0
set it Like:
app:tabMode = "scrollable"
Answer was simple. I just had to add a custom tab
<com.astuetz.PagerSlidingTabStrip
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#id/materialviewpager_pagerTitleStrip"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:pstsPaddingMiddle="true"
app:pstsDividerPadding="20dp"
app:pstsIndicatorColor="#FFF"
app:pstsIndicatorHeight="2dp"
app:pstsShouldExpand="false"
app:pstsTabPaddingLeftRight="10dp"
app:pstsTabTextAllCaps="true"
tools:background="#A333"
/>
Notice that I set pstsShouldExpand to false. This makes sure that the tab width is not distributed, hence we can see full text.
Note - This makes the tab width uneven.

TabLayout scrollable tabs padding is calculated badly

We have this TabLayout in xml:
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
app:tabGravity="fill"
app:tabContentStart="#dimen/tab_content_start"
app:tabIndicatorColor="#color/MaterialDividerColor"
app:tabSelectedTextColor="#color/DarkRed"
app:tabTextColor="#color/Black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
The problem is, when displaying on a tablet it looks really weird, all the tabs have the same width and therefor have each wildly different padding:
Surprisingly, on a phone it works as expected, or at least the problem is not visible:
I am using the 23.2.0 support library, which frankly looks full of bugs.
When you have TabLaout which app:tabMode="scrollable", You need to give some value to tabMinWidth, such as app:tabMinWidth="40dp". Otherwise, tab paddings wouldn't work.

Is there a way to know default TabLayout height from XML

We can know action bar / toolbar height from XML, via ?attr/actionBarSize
Is there a similar thingy for TabLayout height? Something like ?attr/tabLayoutSize
As far as I know, NO.
But the google design specs says that it the height of tabs should be 48dp or 72dp in case of icon + text in a tab
https://www.google.com/design/spec/components/tabs.html#tabs-specs
A bit code would be helpful.
You can set the hight of the tablayout in xml like this:
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:scrollbars="horizontal"
android:layout_width="match_parent"
android:layout_height="#dimen/tablayout_height"
app:layout_scrollFlags="scroll|enterAlways"
app:tabIndicatorHeight="10dp"
app:tabIndicatorColor="#color/blue"
style="#style/TabStyle"/>
And if you for example want to get that height in your code you could get it by doing this:
int height = (int) mContext.getResources().getDimension(R.dimen.tablayout_height);

ViewPager and ViewPagerIndicator inside RelativeLayout making RelativeLayout expand through whole screen

This is what is causing me many problems:
<RelativeLayout
android:id="#+id/galleryLayout"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#color/white">
<android.support.v4.view.ViewPager
android:id="#+id/imageViewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circlePageIndicator"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="#dimen/padding_middle"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Right now this code kind of works only because I've set RelativeLayout's height to 180dp but I don't want to have it this way. I want the whole thing to have height according to ViewPager's child.
There are exactly 2 problems if I set RelativeLayout's height to WrapContent.
Problem 1
ViewPager will expand throughout the whole screen. It just doesn't respect it's wrap_content height attribute. But I've partially solved that with this answer. I'd still appreciate if there's a better solution though.
Problem 2
I want have CirclePageIndicator on the bottom of it's parent (RelativeLayout) so I've added attribute layout_alignParentBottom="true" but now because of this, the RelativeLayout will expand throughout the whole screen for some reason.
So what I'm trying to have is a RelativeLayout which wraps around ViewPager which wraps around it's child. The child is downloaded from web so I can't pre-set it. And on the bottom of that RelativeLayout, I want to have a ViewPagerIndicator.
As for the problem 1 you solved the issue correctly, ViewPager will not wrap its children by default.
As for the second problem, this is a normal RelativeLayout behaviour. If you set its height to wrap_content and add two children, one with layout_alignParentTop="true" and second one with layout_alignParentBottom="true", they will stretch your layout height-wise.
What you should do is: ask yourself if you really need RelativeLayout. If you've provided the whole layout of yours I don't see a need for RelativeLayout (its costly). Vertical LinearLayout would do just fine. If you decide that you really need RelativeLayout, try changing your Indicator's rule from layout_alignParentBottom="true" to android:layout_below="#+id/imageViewPager".

Prevent ActionBar tabs from horizontal scrolling

I have 4 tabs in an actionBar. I'm using a RelativeLayout with an ImageView and a TextView as a customView for the tabs.
The actionBar overflows the width of the screen and the tabs become horizontally scrollable.
The actual width of my customView is not much, but the Tabs take up extra space (varies on different devices).
How can I restrict the tabs to occupy not more than 1/4th of the screen width.
Tab view width is based on content/yourImage size, so to make tab view width smaller, you have to control the content/Image size. You can use CustomView for your Tab by putting ImageView inside FrameLayout.
For detail please go to the answer for post ActionBar tabs set dynamic width according to screen width.
Instead of Using the ActionBar, you will need to either implement a ToolBar or something similar in your UI which you can control the width of that has Tab's inside it...
ActionBar is meant to take up the full width of the Screen whereas you can specify the width of a Toolbar yourself...
create a tab_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="200dp"
android:layout_height="wrap_content"
app:popupTheme="#style/Theme.AppCompat.Light.NoActionBar">
<!-- Add your Tabs here -->
</android.support.v7.widget.Toolbar>
Then just include this layout in your activity:
<include layout="#layout/tab_layout
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

Categories

Resources