Home » Android » How to define textAppearence value in android layout-Exceptionshub

How to define textAppearence value in android layout-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment


In my layout, I am using falling for predefined values for android:textAppearence. But, there are, as I found, at least 2 ways to define them, which yields a very different result (like, different font size and alpha and weight value):

  1. android:textAppearance:="@style/TextAppearance.MaterialComponents.Caption". This @style/... values are defined in (in my case) in a file
    ~/.gradle/caches/transforms-2/files-2.1/11ab83d2971a1126be493aa33fdd0f6e/material-1.1.0/res/values. This, for example, is defined as:

    <style name="TextAppearance.MaterialComponents.Caption" parent="TextAppearance.AppCompat.Caption">
      <item name="fontFamily">sans-serif</item>
      <item name="android:fontFamily">sans-serif</item>
      <item name="android:textStyle">normal</item>
      <item name="android:textAllCaps">false</item>
      <item name="android:textSize">12sp</item>
      <item name="android:letterSpacing">0.0333333333</item>
  2. android:textAppearance="@android:style/TextAppearance.Material.Menu". This is defined in
    /home/rudra/.Android/Sdk/platforms/android-29/data/res/values/styles_material.xml. This is defined as:

    <style name="TextAppearance.Material.Menu">
        <item name="textSize">@dimen/text_size_menu_material</item>
        <item name="fontFamily">@string/font_family_menu_material</item>

The main problem to me is, I am not sure which one to use for more consistent user experience. I am sure, the file for case 2 will not be shipped with my app, but maybe the user will have styles_material for his android-xx. But by realizing how values have changed with material-1.0.0 and 1.1.0, I am a bit sKeptic to depend on these values, but I am not sure.

On the other hand, this is a considerable work to redefine textAppearance for all possible scenarios.

So, I am looking for suggestions on how to define the textAppearence, and among case 1 and case 2, which should I use for more consistent result across devices

How to&Answers:

If you want to define a custom textAppearance, you can define as follow. I use material-components-android.

In style.xml

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryDarkColor</item>
    <item name="colorPrimaryVariant">@color/primaryLightColor</item>
    <item name="colorOnPrimary">@color/primaryTextColor</item>
    <item name="colorSecondary">@color/secondaryColor</item>
    <item name="colorSecondaryVariant">@color/secondaryDarkColor</item>
    <item name="colorOnSecondary">@color/secondaryTextColor</item>

    <!-- This line is important -->
    <item name="textAppearanceHeadline3">@style/TextAppearance.Headline3</item>

  <style name="TextAppearance.Headline3" parent="TextAppearance.MaterialComponents.Headline3">
    <!-- Define your custom appearance here. -->
    <item name="fontFamily">@font/work_sans_black</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:textSize">@dimen/text_size</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/primaryTextColor</item>

You can use this-TextAppearance.Headline3 in layout as follow.


This is my usage for custom text appearance. You can define as you like.

If you want to customize theme or style, you have to know common attributes. You can see it at this article.

I hope this will be helpful for you.


Looking at TextView.java, there don’t appear to be any attributes that are defined by android:textAppearance that are not able to be set on a TextView directly. So there doesn’t appear to be any technical reason that one needs to use them over defining attributes in styles directly, at least in one’s ability to configure how a TextView looks.

This means that you can avoid using it in your layouts as well as your styles without any adverse effects, provided that you override all the associated attributes. The base theme defines many different standard android:textAppearances for the various widgets, so you should check that all the widgets are properly overridden.

Based on my experience, android:textAppearance is most useful if you wish your app to appear to integrate into the rest of the device. So if you need big text, you can use android:textAppearance=”?android:attr/textAppearanceLarge” and now your text is large! No need to know how many sp that means for the given device/screen size/etc..

However if your app is highly styled and you’d otherwise be overriding all the text sizes anyhow, the value-add of android:textAppearance diminishes. You can certainly use it as referenced in the posts above, but if that doesn’t fit into your styling system, then feel free to omit it. It’s just another tool to help you get a good-looking app across all devices.

I would also recommend making extra-sure that you try your app across a variety of devices if you choose not to use it, just to make sure that you didn’t neglect to override a default android:textAppearance.