android font size of tabs

Write these below codes in styles.xml

<style name="MyTabLayout" parent="Base.Widget.Design.TabLayout">
        <item name="tabTextAppearance">@style/MyTabTextAppearance</item>
</style>

<style name="MyTabTextAppearance" parent="TextAppearance.AppCompat.Button">
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="textAllCaps">true</item>
</style>

And in your tablayout, set the style like below.

<android.support.design.widget.TabLayout
     style="@style/MyTabLayout"
     android:layout_width="width"
     android:layout_height="height"/>

try this

Create an xml layout named custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tab"
android:textColor="@color/colorAccent"/>

than in your activity set text size programaticlly like below code

TextView tabOne = (TextView) 
LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("ONE");
tabOne.setTextSize(14); // set font size as per your requirement 
tabLayout.getTabAt(0).setCustomView(tabOne);

If you want to change the font size programmatically, you can use java reflection to access the integer field tabTextSize in the TabLayout class and set the font size as per your requirement.

public static void updateTabLayoutFontSize(TabLayout tabLayout, int textSizeInPixel) {
  try {
     Field mCursorDrawableRes = TabLayout.class.getDeclaredField("tabTextSize");
     mCursorDrawableRes.setAccessible(true);
     mCursorDrawableRes.set(tabLayout, textSizeInPixel);
  } catch (Exception e) {
    Log.d("TAG1", "Failed to update tablayout font using reflection");
  }
}

Leave a Comment