android – How to change the color of a button?

android – How to change the color of a button?

The RIGHT way…

The following methods actually work.

if you wish – using a theme
By default a buttons color is android:colorAccent. So, if you create a style like this…

<style name=Button.White parent=ThemeOverlay.AppCompat>
    <item name=colorAccent>@android:color/white</item>

You can use it like this…

    [email protected]/Button.White

alternatively – using a tint
You can simply add android:backgroundTint for API Level 21 and higher, or app:backgroundTint for API Level 7 and higher.

For more information, see this blog.

The problem with the accepted answer…

If you replace the background with a color you will loose the effect of the button, and the color will be applied to the entire area of the button. It will not respect the padding, shadow, and corner radius.

You can change the colour two ways; through XML or through coding. I would recommend XML since its easier to follow for beginners.


    [email protected]:color/white
    [email protected]:color/black

You can also use hex values ex.

android:[email protected]:color/white


//btn represents your button object


android – How to change the color of a button?

For the text color add:

android:textColor=<hex color>

For the background color add:

android:background=<hex color>

From API 21 you can use:

android:backgroundTint=<hex color>

Note: If youre going to work with android/java you really should learn how to google 😉
How to customize different buttons in Android

Leave a Reply

Your email address will not be published.