Create TabLayout text in bold

I am using TabLayout from the Android Design support library and want to style its text (title). In particular, make it bold. How to achieve this only in XML ?

<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" /> 
+8
source share
5 answers

First you need to add this to styles.xml:

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

Even if you do not want to resize the text, you must include it in the styles, otherwise nothing will be shown.

Then the style should be applied to the TabLayout using the app:tabTextAppearance , not style !

 <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 allcaps, you can add the following to TabLayoutTextStyle :

 <item name="android:textAllCaps">true</item> 
+26
source
  • One option is to add to styles.xml

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

    inside "TextAppearance.Design.Tab" with the same name as the 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> 
  • Another option: inside your layout directly to your style - let's say you name it myTabLayoutStyle

      style="@style/myTabLayoutStyle" 

and inside this style is redirected again to another text-only style:

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

like this:

  <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> 
+3
source

You need to declare the following styles

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

Now you can simply use it like this:

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

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

Add TabLayout text style to styles.xml


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

And set TabLayoutTextStyle as the style for the TabLayout properties.


 <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" style="@style/TabLayoutTextStyle" /> 
0
source

This is the right way to do this.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:baselineAligned="false"> <android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="50dp" app:tabTextAppearance="@style/TextAppearance.Bold" app:tabTextColor="@color/grey" app:tabSelectedTextColor="@color/black" /> </LinearLayout> 
0
source

Source: https://habr.com/ru/post/1243798/


All Articles