Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Tab layout: Wrap tab indicator width with respect to tab title

Is there any way to wrap tab indicator width with respect to tab title ?

Current

Required

like image 437
Narayan C.R Avatar asked Nov 08 '16 06:11

Narayan C.R


People also ask

How do I set tab layout?

This example demonstrates how do I create a Tab Layout in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 3 − Add the following code to res/layout/activity_main. xml.

How do I change tab position in android programmatically?

By default if you select a tab it will be highlighted. If you want to select Explicitly means use the given commented code under onTabSelected(TabLayout. Tab tab) with your specified tab index position. This code will explains about change fragment on tab selected position using viewpager.


2 Answers

Yes, it's possible to wrap tab indicator as title setting padding to 0

 public void wrapTabIndicatorToTitle(TabLayout tabLayout, int externalMargin, int internalMargin) {         View tabStrip = tabLayout.getChildAt(0);         if (tabStrip instanceof ViewGroup) {             ViewGroup tabStripGroup = (ViewGroup) tabStrip;             int childCount = ((ViewGroup) tabStrip).getChildCount();             for (int i = 0; i < childCount; i++) {                 View tabView = tabStripGroup.getChildAt(i);                 //set minimum width to 0 for instead for small texts, indicator is not wrapped as expected                 tabView.setMinimumWidth(0);                 // set padding to 0 for wrapping indicator as title                 tabView.setPadding(0, tabView.getPaddingTop(), 0, tabView.getPaddingBottom());                 // setting custom margin between tabs                 if (tabView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {                     ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) tabView.getLayoutParams();                     if (i == 0) {                         // left                         settingMargin(layoutParams, externalMargin, internalMargin);                     } else if (i == childCount - 1) {                         // right                         settingMargin(layoutParams, internalMargin, externalMargin);                     } else {                         // internal                         settingMargin(layoutParams, internalMargin, internalMargin);                     }                 }             }              tabLayout.requestLayout();         } }  private void settingMargin(ViewGroup.MarginLayoutParams layoutParams, int start, int end) {     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {         layoutParams.setMarginStart(start);         layoutParams.setMarginEnd(end);         layoutParams.leftMargin = start;         layoutParams.rightMargin = end;     } else {         layoutParams.leftMargin = start;         layoutParams.rightMargin = end;     } } 

Result

EDIT: As of com.android.support:design:28.0.0, you can adjust the indicator as label easily now setting:

app:tabIndicatorFullWidth="false"

EDIT July 2019: Use the material dependency com.google.android.material:material:x.x.x

like image 141
leon Avatar answered Oct 22 '22 05:10

leon


As of support library 28 you can do the following:

app:tabIndicatorFullWidth="false" app:tabPaddingStart="25dp" app:tabPaddingEnd="25dp" 

You can set the desired padding that affects the tab indicator.

Also you can now do this:

app:tabIndicator="@drawable/tab_indicator" 

this will set a custom drawable as indicator.

example of custom drawable:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">      <solid android:color="@color/colorPrimary"/>     <corners android:radius="5dp"/> </shape> 
like image 24
Malek Hijazi Avatar answered Oct 22 '22 05:10

Malek Hijazi