How to set 2 tab takes equal width in tablayout
Asked Answered
I

8

7

I am adding 2 tabs in the TabLayout, and following is my code.

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tabLayout"
    style="@style/AppTabLayout"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_gravity="center"
    app:tabBackground="@drawable/tab_selector_statistics"
    android:background="@drawable/tab_statistics"
    app:tabContentStart="24dp"
    app:tabGravity="fill"
    app:tabMode="fixed"
    app:tabSelectedTextColor="@color/white"
    app:tabTextColor="@color/black"
    app:layout_constraintBottom_toBottomOf="@+id/view5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/view5"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true" />

I am getting the following output.

enter image description here

But I want tabs to take the full width of the screen, as follows.

enter image description here

Following is my AppTabLayout style defined in styles.xml file.

<style name="AppTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">@null</item>
    <item name="tabIndicatorHeight">1dp</item>
    <item name="tabPaddingStart">16dp</item>
    <item name="tabPaddingEnd">16dp</item>
    <item name="tabSelectedTextColor">@color/white</item>
</style>
Iman answered 16/2, 2019 at 19:36 Comment(5)
Can you try with android:layout_width="0dp"? match_parent is not supported inside ConstraintLayout.Chantry
@EugenPechanec : Tanks for the reply, I tried 0dp but still getting the same output.Iman
@Kirmani88 answer below is correct, need to add just one line app:tabMaxWidth="0dp", if not, you should show whole layoutJink
have you try this: https://mcmap.net/q/713185/-different-tab-width-for-tablayoutRegurgitation
Use SlidingTabLayout.Jugoslavia
P
12

Just add the following.

            android:layout_width="match_parent"
            android:layout_height="30dp"
            app:tabMaxWidth="0dp"
            app:tabGravity="fill"
            app:tabMode="fixed"
Politico answered 17/2, 2019 at 10:10 Comment(0)
K
1

Use style

<style name="MyTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabGravity">fill</item>
    <item name="tabMaxWidth">0dp</item>
</style>

or

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

reference

Full width tablayout

Kibbutznik answered 23/2, 2019 at 9:58 Comment(0)
P
0

match_parent is not supported by ConstraintLayout. Set width to 0dp to let it match constraints.

android:layout_width="0dp"

From the official doc:

Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".

Photothermic answered 23/2, 2019 at 7:19 Comment(0)
H
0

You should be use stretchColumns in Table Layout

<TableLayout
        android:stretchColumns="*">
        <TableRow
            android:layout_width="0dp">
    <TableRow
            android:layout_width="0dp">
</TableLayout>
Herrin answered 23/2, 2019 at 9:43 Comment(0)
P
0

Just do as below. This is work for me.

<android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabBackground="@color/colorPrimary"
                app:tabIndicatorColor="@android:color/white"
                app:tabTextColor="@android:color/white"
                app:tabMode="fixed"
                app:tabGravity="fill"/>
Predicable answered 23/2, 2019 at 10:10 Comment(0)
R
0

You may also want to use app:tabIndicatorFullWidth="true" in the TabLayout

Rubadub answered 24/6, 2021 at 16:39 Comment(0)
K
0

In my case, I succeeded in getting the accepted output with just this one line of code: app:tabMode="fixed".

Full code of my TabLayout:

<com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:paddingBottom="2dp"
            app:tabMode="fixed"
            app:tabTextColor="?titleTextColor"
            android:background="?android:statusBarColor" />

The Output of my code: 👇

The Output of my code:

Knothole answered 19/7, 2023 at 15:58 Comment(0)
A
-1
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="0dp"
        android:layout_height="34.5dp"
        android:elevation="@dimen/dimen_10"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/overTimeCV"
        app:tabMode="fixed"/>

Check with this TabLayout along with viewPager

Anatollo answered 26/2, 2019 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.