I'd like to change the color of a standard Android button slightly in order to better match a client's branding.
The best way I've found to do this so far is to change the Button
's drawable to the drawable located in res/drawable/red_button.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/red_button_pressed" />
<item android:state_focused="true" android:drawable="@drawable/red_button_focus" />
<item android:drawable="@drawable/red_button_rest" />
</selector>
But doing that requires that I actually create three different drawables for each button I want to customize (one for the button at rest, one when focused, and one when pressed). That seems more complicated and non-DRY than I need.
All I really want to do is apply some sort of color transform to the button. Is there an easier way to go about changing a button's color than I'm doing?
The DroidUX component library has a
ColorButton
widget whose color can be changed easily, both via xml definition and programmatically at run time, so you can even let the user to set the button's color/theme if your app allows it.You can set theme of your button to this
In
<Button>
useandroid:background="#33b5e5"
. or betterandroid:background="@color/navig_button"
Mike, you might be interested in color filters.
An example:
try this to achieve the color you want.
There is a much easier way now : android-holo-colors.com
It will let you change the colors of all holo drawables (buttons, spinners, ...) easily. You select the color and then download a zip file containing drawables for all resolutions.
values\styles.xml
then: