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?
I discovered that this can all be done in one file fairly easily. Put something like the following code in a file named
custom_button.xml
and then setbackground="@drawable/custom_button"
in your button view:This is my solution which perfectly works starting from API 15. This solution keeps all default button click effects, like material
RippleEffect
. I have not tested it on lower APIs, but it should work.All you need to do, is:
1) Create a style which changes only
colorAccent
:2) Add these two lines to your
button
widget:Sample Button widget
I like the color filter suggestion in previous answers from @conjugatedirection and @Tomasz; However, I found that the code provided so far wasn't as easily applied as I expected.
First, it wasn't mentioned where to apply and clear the color filter. It's possible that there are other good places to do this, but what came to mind for me was an OnTouchListener.
From my reading of the original question, the ideal solution would be one that does not involve any images. The accepted answer using custom_button.xml from @emmby is probably a better fit than color filters if that's your goal. In my case, I'm starting with a png image from a UI designer of what the button is supposed to look like. If I set the button background to this image, the default highlight feedback is lost completely. This code replaces that behavior with a programmatic darkening effect.
I extracted this as a separate class for application to multiple buttons - shown as anonymous inner class just to get the idea.
Use it in this way:
An easy way is to just define a custom Button class which accepts all the properties that you desire like radius, gradient, pressed color, normal color etc. and then just use that in your XML layouts instead of setting up the background using XML. A sample is here
This is extremely useful if you have a lot of buttons with same properties like radius, selected color etc. You can customize your inherited button to handle these additional properties.
Result (No Background selector was used).
Normal Button
Pressed Button
You can now also use appcompat-v7's AppCompatButton with the
backgroundTint
attribute: