7

I have created several styles for font in my app.

How can I add these styles to views? - 1) Using style attribute 2) Using textAppearance.

To use style is not an option because views may have other attributes (margins, paddings, min width, etc - I cant specify 2 styles for a view), so I need to specify text-related attributes separately, but android:textColor doesnt work here:

styles.xml:

<style name="TextAppearance.baseText" parent="@android:style/TextAppearance">
    <item name="android:textAppearance">?android:textAppearanceSmall</item>
    <item name="android:typeface">sans</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">15sp</item> 
</style>

layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="65dp"
      android:orientation="horizontal" >

      <Button
           android:id="@+id/backButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/sometext"
           android:textAppearance="@style/TextAppearance.baseText"/>
</RelativeLayout>

Why doesnt it work? How can I specify textcolor in textappearance?

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Eugene Chumak
  • 3,272
  • 7
  • 34
  • 52

3 Answers3

13

As Oderik has mentioned in the comments, the Button view has a default value for it's textColor attribute. This textColor value overrides whatever value is set through the textAppearance attribute. Therefore you have two options:

  • Set the textColor to @null in the style:

    <style name="Application.Button">   
      <item name="android:textAppearance">@style/Application.Text.Medium.White</item>
      <item name="android:textColor">@null</item> 
    </style>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Application.Button" />
    
  • Set the textColor to @null in the Button XML:

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/Application.Button"   
    android:textColor="@null" />
    
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
TheIT
  • 11,919
  • 4
  • 64
  • 56
8

It works. Your text color is white - the same as default color. Put there any other color like <item name="android:textColor">#AA6699</item> and you will see difference. Second thing is that you are trying to set text appearance in text appearance - doen's make sense. If you want to set small letters do this using parent parent="@android:style/TextAppearance.Small and delete line <item name="android:textAppearance">?android:textAppearanceSmall</item> Third thing is that you want to have small letters and put additional textSize - doesn't make sense. Try this, works for me:

<style name="BaseText" parent="@android:style/TextAppearance.Small">
        <item name="android:typeface">sans</item>
        <item name="android:textColor">#AA7755</item>
  </style>

If you want to set everything in style:

<style name="BaseText" parent="@android:style/TextAppearance.Large">
        <item name="android:typeface">sans</item>
        <item name="android:textColor">#AA7755</item>
    </style>

   <style name="BaseText.Margins">
       <item name="android:layout_margin">10dip</item>
   </style>

Where BaseText.Margins inherits from BaseText. Then you can just use:

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        style="@style/BaseText.Margins" />
ania
  • 2,352
  • 1
  • 20
  • 20
  • android:textColor doesnt work for me. I tried BaseText on Api8 and Api15. Concerning BaseText.Margins approach - I'm aware of it, but it means creating several styles if several views with different appearance have the same textStyle. It may result in large amount of styles - I would prefer to avoid that. – Eugene Chumak Jun 27 '12 at 14:07
  • Ok, so your problem is not style, your problem is that you want to set it to button. The easiest way to get what you want is to put TextView instead Button and make it clickable using selectors. Then you can use your style :) – ania Jun 27 '12 at 14:49
  • Yeap! That really worked, thank you! To be honest, I thought that it all might be due to the fact that I use button instead of textview... but I didn't believe that could be true and didn't test it, cause button extends textview) – Eugene Chumak Jun 27 '12 at 16:34
  • That's great. Buttons has some problems with styling. If you are interested in other ways how to do this you can find some examples how to create themes in application but it is much more compicated. – ania Jun 27 '12 at 17:06
  • 10
    The default Button style explicitly sets a text color which has higher priority than a text color set via textAppearance. To circumvent this, modify the button style to use no explicit text color by setting the attribute to `@null`. – Oderik Sep 09 '13 at 08:34
7

If you want to use android:TextAppearance instead of style: to set your android:textColor, you need to set android:textColor=@null on your TextView.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium.MySubheader"
    android:textColor="@null"
    tools:text="Section" />

Then it will inherit correctly from your android:TextAppearance

<style name="TextAppearance.AppCompat.Medium.MySubheader">
    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/black_54</item>
</style>
ZakTaccardi
  • 12,212
  • 15
  • 59
  • 107