Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: set textColor with textAppearance attribute

Tags:

text

android

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?

like image 278
Eugene Chumak Avatar asked Jun 27 '12 12:06

Eugene Chumak


People also ask

How do I change text color in programmatically?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

How to Add styles XML file in android studio?

Create and apply a style To create a new style or theme, open your project's res/values/styles. xml file. For each style you want to create, follow these steps: Add a <style> element with a name that uniquely identifies the style.

What is TextAppearance in Android?

TextAppearance styles can be seen as the Android equivalent of Material Design type styles. For custom styles, we recommend two approaches to help separate concerns and create a single source of truth for type theming values in your app: Store all TextAppearance styles in a single res/values/type. xml file.


2 Answers

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" />
    
like image 59
TheIT Avatar answered Oct 31 '22 08:10

TheIT


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" />
like image 25
ania Avatar answered Oct 31 '22 07:10

ania