Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit the tab width screen in android

I implement the tab layout using android support library and it looks like

enter image description here

Here the tab width is not fit with screen and my layout is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white" />

</LinearLayout>
like image 292
azhar Avatar asked Nov 29 '22 10:11

azhar


2 Answers

A "simpler" answer would just be adding app:tabMaxWidth="0dp" in your TabLayout 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" />
like image 30
Ashraf Amin Avatar answered Dec 15 '22 21:12

Ashraf Amin


Try calling those methods on your TabLayout:

tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
like image 146
Bartek Lipinski Avatar answered Dec 15 '22 22:12

Bartek Lipinski