Setting Button Text Color with a Style
Asked Answered
F

1

13

I have not been able to set the text color of a Button using a predefined style. I must be missing something simple. For example, I have a button:

    <Button
    android:id="@+id/calculate_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/calculate"
    android:background="@drawable/rounded_bottom_shape"
    android:padding="5dp"
    android:textAppearance="@style/CalcResultStyle">
    </Button>

And the corresponding style is:

<style name="CalcResultStyle">
    <item name="android:textSize">12sp</item>
    <item name="android:textColor">#FFF</item>
</style>

The size portion works fine, but the color does not. The only workaround I have found is to set the textColor on the actual button, which means changing colors on a number of buttons becomes a pain. I also prefer to set the textColor attribute in my styles using a color reference such as @color/Brown for example, but setting the color using a reference or explicitly seems to make no difference.

Forethought answered 23/9, 2011 at 2:25 Comment(2)
Actually by default, buttons text is black. Please read my question.Forethought
Ah that's right, list views and other items are black with white text.Gothart
A
15

I use this. Maybe it will help you. Can you please tell me what you wrote in rounded_bottom_shape?

<Button
android:id="@+id/calculate_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Calculate"
android:background="#454545"
android:padding="5dp"
style="@style/CalcResultStyle">
</Button>
Antipodal answered 23/9, 2011 at 4:28 Comment(2)
Weird...but it does work. I would have never thought to try losing the android tag. None-the-less..thank you.Forethought
styles propagate through the view hierachy, standard attributes do not. a Button does not have a textAppearance attribute so it does nothing.Bertle

© 2022 - 2024 — McMap. All rights reserved.