How to set Tab width in Tab layout?

user5912784 picture user5912784 · Feb 11, 2016 · Viewed 25k times · Source

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.

Answer

HassanUsman picture HassanUsman · Feb 11, 2016

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