Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android font size of tabs

Tags:

android

tabs

i ask this quest for a few time ago , but i get no solutions :( my problem is, that i have an android app with a tab activity, where i have to set the font size of my tabs, but i don't know how.

in my activity i set my tabs programmatically:

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("MY TAB 1"));
    tabLayout.addTab(tabLayout.newTab().setText("MY TAB 2"));
    tabLayout.addTab(tabLayout.newTab().setText("MY TAB 3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

Problem is, that the last 1 - 2 letters will be cut, so i have to set the font size smaller, but how?

i hope anyone can help me.

like image 874
Ghost108 Avatar asked Dec 09 '15 07:12

Ghost108


People also ask

How do I use TabLayout on Android?

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.


3 Answers

if you set text_size directly in style of tab layout, it doesn't work!! you have to set it into a style which has parent="@android:style/TextAppearance.Widget.TabWidget"

<style name="tab_text" parent="@android:style/TextAppearance.Widget.TabWidget">
    <item name="android:fontFamily">@fonts/iransans_b</item>
    <item name="android:textSize">@dimen/textSize_Large</item>
    <item name="textAllCaps">false</item>
</style>

<style name="Tab" parent="Widget.Design.TabLayout">
    <item name="tabTextAppearance">@style/tab_text</item>
</style>


 <com.google.android.material.tabs.TabLayout
                 android:id="@+id/item_tabs"
                 android:layout_width="match_parent"
                 android:layout_height="@dimen/margin_dp_65"
                 style="@style/Tab"/>

and also this is a full style for tab:

 <style name="Tab_WithBackground" parent="Widget.Design.TabLayout">
    <item name="tabSelectedTextColor">@color/purple</item>
    <item name="tabTextColor">@drawable/tab_text_color</item>
    <item name="tabIndicatorColor">@color/white</item>
    <item name="tabGravity">fill</item>
    <item name="tabIndicatorHeight">4dp</item>
    <item name="android:tabStripEnabled">true</item>
    <item name="android:padding">0dp</item>
    <item name="tabMaxWidth">0dp</item>
    <item name="android:minHeight">@dimen/margin_dp_80</item>
    <item name="tabTextAppearance">@style/tab_text</item>
</style>
like image 140
Maryam Azhdari Avatar answered Sep 24 '22 05:09

Maryam Azhdari


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"/>
like image 36
Nigam Patro Avatar answered Sep 22 '22 05:09

Nigam Patro


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");
  }
}
like image 24
Muhammed Afsal Avatar answered Sep 22 '22 05:09

Muhammed Afsal