0

I would like to create a button style which all of my buttons use, this way I do not need to set the android:textColor on each one.

I have this styling in a file named defaultbutton.xml

<item android:state_pressed="false" android:state_focused="false">
    <shape android:shape="rectangle">
        <solid android:color="#F3C22C"/>
        <corners android:radius="10dp" />
    </shape>
    <color android:color="@color/brown"></color>
</item>

And for a test button I set:

android:background="@drawable/defaultbutton"

The button gets the background color but the text color stays as black.

What am I doing wrong?

andrewb
  • 2,995
  • 7
  • 54
  • 95
  • Check this post: http://stackoverflow.com/questions/4692642/android-customized-button-changing-text-color – Willis Mar 09 '15 at 18:01

3 Answers3

1

You need to add color attribute to the item tag to set color of text.

/res/drawable/custom_color.xml

<item android:state_pressed="false" android:state_focused="false"
      android:color="@color/text_color"> //this is how text color can be defined

</item>

Now set textColor to the textView of your layout

<TextView  ...
    android:textColor="@drawable/custom_color">
Apurva
  • 7,871
  • 7
  • 40
  • 59
  • So I still need to set the textColor attribute at the element level, which is what I did not want to have to do – andrewb Mar 09 '15 at 18:18
  • Yes @andrewb you will have to. And if you want to style your button's background then the xml you have posted is right to style background. You need to add it also, such as, `android:background="your_style"` – Apurva Mar 09 '15 at 18:20
0

You can set it with style:

<style name="MyButtonText" parent="@android:style/Widget.Button">
<item name="android:textColor">@color/mycolor</item>
Djordje Tankosic
  • 1,975
  • 2
  • 20
  • 21
0

You can go to "styles.xml" file, you can find it in "values" folder and add the following lines to the file :

<style name="buttonstyle" >
        <item name="android:textColor" >#ff11</item>
</style>

after that when you add button add this line to your button attr.

style="@style/buttonstyle"
b1programmer
  • 189
  • 1
  • 5