How to set Tab width in Tab layout?
Asked Answered
C

2

36

I'm trying to create a tab layout which has two tabs. When I run the app on small mobile the tab layout looks fine but when I run the same application on Tablet it shows like this.

It looks like this on Tablet:

enter image description here

I want each tab to occupy entire space without any gaps on both the ends. I have created tab layout like this...

private void drawTabLayout() {
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.addTab(tabLayout.newTab().setText("Imprint") );
    tabLayout.addTab(tabLayout.newTab().setText("Legal"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabMode(TabLayout.MODE_FIXED);
    tabLayout.setSelectedTabIndicatorHeight(10);
    tabLayout.setBackgroundColor(Color.WHITE);
}

I have searched many sites but couldn't find the right answer.

Cabby answered 11/2, 2016 at 10:24 Comment(5)
from what I know either you can share equally or squeeze . Otherwise you will have to provide custom layout. Try overriding onMeasure, setting customView (your tab view)Dearly
Add your tabs width to fillParent in xml fileCons
<android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMaxWidth="0dp" app:tabGravity="fill" app:tabMode="fixed" />Dearly
Thank you soo much :) it worked fine @DearlyCabby
no worries I've had this problem with tablet months ago...Dearly
C
63

Add this code in your tab_layout.xml

<android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMaxWidth="0dp"
            app:tabGravity="fill"
            app:tabMode="fixed" />

Hope it will works for you...

Cons answered 11/2, 2016 at 10:47 Comment(0)
A
0

You can create a tab layout where each tab has a custom View and add that tab in the tablayout with whatever size of your choice

View customView = LayoutInflater.from(getContext()).inflate(R.layout.tab_text, null); 

addTab(newTab().setCustomView(customView))
Ardel answered 11/2, 2016 at 10:50 Comment(1)
It does not affect tab layout item, it just change view in viewpagerPleasurable

© 2022 - 2024 — McMap. All rights reserved.