Is there any way to wrap tab indicator width with respect to tab title ?
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.
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.
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; } }
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With