jsmith
jsmith

Reputation: 4897

TextColor defined in style being applied to TextView, but not Button?

I've defined some style resources that include TextAppearance with a defined TextColor. I then apply the styles to some TextViews and Buttons. All the styles come through with the TextView, but not the Button. For some reason, the textColor attribute is not showing up. Is this a bug, or am I missing something in the case of the Button?

Here is the style definition:

<?xml version="1.0" encoding="UTF-8"?>
<resources>

    <style name="TestApp">      
    </style>

    <!-- Text Appearances -->
    <style name="TestApp.TextAppearance">
        <item name="android:typeface">sans</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">16px</item>       
        <item name="android:textColor">#6666FF</item>
    </style>

    <!-- Widget Styles -->
    <style name="TestApp.Widget">
        <item name="android:layout_margin">3sp</item>
    </style>

    <style name="TestApp.Widget.Label">
        <item name="android:textAppearance">@style/TestApp.TextAppearance</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="TestApp.Widget.Switch">
        <item name="android:textAppearance">@style/TestApp.TextAppearance</item>
        <item name="android:layout_width">100px</item>
        <item name="android:layout_height">100px</item>
    </style>

</resources>

and here's the layout where I attempt to apply them:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<TextView
    style="@style/TestApp.Widget.Label"
    android:text="This is my label." />
<TextView
    style="@style/TestApp.Widget.Label"
    android:text="This is my disabled label."
    android:enabled="false" />
<Button
    style="@style/TestApp.Widget.Switch"
    android:text="This is my switch." />
<Button
    style="@style/TestApp.Widget.Switch"
    android:text="This is my disabled switch."
    android:enabled="false" />
</LinearLayout>

Upvotes: 6

Views: 4070

Answers (3)

jsmith
jsmith

Reputation: 4897

Apparently the way to achieve this is to override the textColor attribute in the button styling. Android does it this way in their Global Theme Styles:
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml

I'd love to understand why this is the case.

Upvotes: 3

TheIT
TheIT

Reputation: 12219

For the case of Buttons, there are two ways to define text color through attributes: textColor and through a style defined by textAppearance.

The value set by textColor (which is set by the default style) will override any text color value set by the textAppearance style. Therefore you must set the textColor attribute to @null in one of two ways.

  • 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" />
    

Upvotes: 2

jjb
jjb

Reputation: 3570

There appears to be a textAppearanceButton as well:

http://developer.android.com/reference/android/R.attr.html#textAppearanceButton

You may want to try that.

Upvotes: 0

Related Questions