Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making TabLayout text bold

I'm using the TabLayout from the Android Design Support library and want to style its text (title). Specifically making it bold. How to achieve that in XML only?

<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
app:tabTextColor="@color/white"
app:tabSelectedTextColor="@color/white"
app:tabIndicatorColor="@color/accent"
android:layout_height="wrap_content"
app:tabIndicatorHeight="3dp" />
like image 664
Vahid Amiri Avatar asked Feb 24 '16 21:02

Vahid Amiri


2 Answers

First this must be added to the styles.xml:

<style name="TabLayoutTextStyle">
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">bold</item>
</style>

Even if you don't want to alter the text size, you must include it in the styles otherwise nothing will be shown.

Then the style must be applied to the TabLayout using app:tabTextAppearance not the style attribute!

<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
app:tabTextColor="@color/white"
app:tabSelectedTextColor="@color/white"
app:tabIndicatorColor="@color/accent"
android:layout_height="wrap_content"
app:tabIndicatorHeight="3dp" 
app:tabTextAppearance="@style/TabLayoutTextStyle" />

To enable the allcaps you may add the following to the TabLayoutTextStyle:

<item name="android:textAllCaps">true</item>
like image 192
Vahid Amiri Avatar answered Oct 19 '22 11:10

Vahid Amiri


  1. One option is to add In styles.xml

      <item name="android:textStyle">bold</item> 
    

    inside "TextAppearance.Design.Tab" with same name as parent

    <style name="TextAppearance.Design.Tab" parent="TextAppearance.Design.Tab">
      <item name="android:textSize">15sp</item>
      <item name="android:textStyle">bold</item>
      <item name="android:textColor">?android:textColorSecondary</item>
      <item name="textAllCaps">true</item>
      <item name="android:singleLine">true</item>
    </style>
    
  2. Other option : inside your layout direct to your style - lets say you call it myTabLayoutStyle

     style="@style/myTabLayoutStyle"
    

and inside that style redirect again to other style just for text appearance :

      <item name="tabTextAppearance">@style/myTabTextStyle</item>

like that:

  <android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    style="@style/myTabLayoutStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="600dp"
    android:minHeight="?attr/actionBarSize"
    app:tabGravity="fill"
    android:singleLine="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

inside styles.xml:

  <style name="myTabLayoutStyle" parent="Widget.Design.TabLayout">
    <item name="tabMaxWidth">@dimen/tab_max_width</item>
    <item name="tabIndicatorColor">?attr/colorAccent</item>
    <item name="tabIndicatorHeight">4dp</item>
    <item name="tabPaddingStart">3dp</item>
    <item name="tabPaddingEnd">3dp</item>
    <item name="android:singleLine">true</item>
    <item name="tabBackground">?attr/selectableItemBackground</item>
    <item name="tabSelectedTextColor">?android:textColorPrimary</item>
    <item name="tabTextAppearance">@style/myTabTextStyle</item>
  </style>

   <style name="myTabTextStyle">
       <item name="android:textSize">15sp</item>
       <item name="android:textStyle">bold</item>
       <item name="android:textColor">?android:textColorSecondary</item>
       <item name="textAllCaps">true</item>
       <item name="android:singleLine">true</item>
  </style>
like image 27
Udi Reshef Avatar answered Oct 19 '22 11:10

Udi Reshef