android – How to change the color of a button?-ThrowExceptions

Exception or error:

I’m new to android programming. How do I change the color of a button?

    android:text="Button Text"
How to solve:

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



You can also use hex values ex.



//btn represents your button object



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…


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.


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 you’re going to work with android/java you really should learn how to google 😉
How to customize different buttons in Android


Here is my code, to make different colors on button, and Linear, Constraint and Scroll Layout

First, you need to make a custom_button.xml on your drawable

  1. Go to res
  2. Expand it, right click on drawable
  3. New -> Drawable Resource File
  4. File Name : custom_button, Click OK

Custom_Button.xml Code

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
    <item android:state_pressed="true" android:drawable="@color/red"/> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->
    <item android:drawable="@color/black"/> <!-- default -->

Second, go to res

  1. Expand values
  2. Double click on colors.xml
  3. Copy the code below

Colors.xml Code

<?xml version="1.0" encoding="utf-8"?>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="black">#000</color>
    <color name="violet">#9400D3</color>
    <color name="indigo">#4B0082</color>
    <color name="blue">#0000FF</color>
    <color name="green">#00FF00</color>
    <color name="yellow">#FFFF00</color>
    <color name="orange">#FF7F00</color>
    <color name="red">#FF0000</color>

Screenshots below

enter image description here
XML Coding
enter image description here
Design Preview


Through Programming:


and your colors.xml must contain…

<?xml version="1.0" encoding="utf-8"?>
 <color name="colorOffWhite">#80ffffff</color>


To change the color of button programmatically

Here it is :

Button b1;
//colorAccent is the resource made in the color.xml file , you can change it.

Leave a Reply

Your email address will not be published. Required fields are marked *