1

I added tab dynamically if tab is 4 then it is display well but if tab is 2 or 3 then it is not feet to match parent.

Code

 public void setUpTab(TabLayout tabLayout, String count, String typeId) {

    TabLayout.Tab tab = tabLayout.newTab();

    RelativeLayout relativeLayout = (RelativeLayout)
            LayoutInflater.from(this).inflate(R.layout.custom_tab, tabLayout, false);
    TextView tabTextView = (TextView) relativeLayout.findViewById(R.id.txt_tab);
    tabTextView.setText(count);
    tab.setTag(typeId);
    tab.setCustomView(tabTextView);
    tabLayout.addTab(tab);
}

Xml Code:

 <android.support.design.widget.TabLayout
    android:id="@+id/tabsRoleType"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/com_facebook_likeboxcountview_border_radius"
    app:tabBackground="@drawable/tab_rounded_background"
    app:tabGravity="fill"
    app:tabIndicatorHeight="0dp"
    app:tabSelectedTextColor="@color/WhiteColor"
    app:tabTextAppearance="@style/TextAppearance.Design.Tab"
    app:tabTextColor="@color/BlackColor" />

enter image description here

Ko Vartthan
  • 375
  • 3
  • 21
Mehul Tank
  • 195
  • 13

2 Answers2

1

Add/Update following lines in XML TabLayout

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

Good practice is app:tabMode="scrollable"

Ganesh Pokale
  • 1,503
  • 10
  • 23
1

Add android:tabMaxWidth , android:tabMode and android:tabgravity

Sample XML Code:

 <android.support.design.widget.TabLayout
    android:id="@+id/tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:tabIndicatorColor="@color/colorPrimary"
    app:tabIndicatorHeight="3dp"
    app:tabMaxWidth="0dp"
    app:tabGravity="fill"
    app:tabMode="fixed"
    app:tabSelectedTextColor="@color/colorPrimary"
    app:tabTextAppearance="@style/TabLayoutTextStyle"
    app:tabTextColor="@color/textCol" />
Ko Vartthan
  • 375
  • 3
  • 21