jsmith jsmith - 3 years ago 124
Android Question

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"?>

<style name="TestApp">

<!-- 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>

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

<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 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>


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:layout_height="fill_parent" >
android:text="This is my label." />
android:text="This is my disabled label."
android:enabled="false" />
android:text="This is my switch." />
android:text="This is my disabled switch."
android:enabled="false" />

Answer Source

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:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download