80

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

<Button
    android:id="@+id/btn"
    android:layout_width="55dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:text="Button Text"
    android:paddingBottom="20dp"/>
Zoe
  • 27,060
  • 21
  • 118
  • 148
hahoanghai.ueh
  • 828
  • 1
  • 6
  • 9
  • 9
    @theapache64 Maybe I don't research properly. But, I think subtle changes of UI for Android SDK and Material Design are documented not so straightly or even poorly. It's like sometimes you should rely on IDE hints, sometimes on documentation, sometimes on some stuff from forums like Stack Overflow. I came here after seeking in Material Design documentation. – Daniel Galion Oct 23 '20 at 11:40

24 Answers24

103

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>
</style>

You can use it like this...

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/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.

Nechemia Hoffmann
  • 2,118
  • 2
  • 12
  • 21
51

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

XML:

<Button
    android:background="@android:color/white"
    android:textColor="@android:color/black"
/>

You can also use hex values ex.

android:background="#FFFFFF"

Coding:

//btn represents your button object

btn.setBackgroundColor(Color.WHITE);
btn.setTextColor(Color.BLACK);
DevineDecrypter
  • 151
  • 1
  • 16
Aicha Hezit
  • 560
  • 4
  • 5
  • 2
    Would like to point out that its a good idea to use layout XML for this regardless of your experience level, especially if the color doesn't need to change. Hell even if it does change I would use layout binding. – BillHaggerty Jun 26 '17 at 18:47
  • 11
    don't use this - you'll lose the button animation (lift and ripple) – Someone Somewhere Mar 14 '19 at 12:22
  • @SomeoneSomewhere that's right. See [the correct answer](https://stackoverflow.com/a/55954610/7711481). – Nechemia Hoffmann May 02 '19 at 14:29
33

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>"
android:backgroundTintMode="<mode>"


How to customize different buttons in Android

max pleaner
  • 26,189
  • 9
  • 66
  • 118
Tomblarom
  • 1,429
  • 1
  • 15
  • 38
  • 1
    @PayelSenapati kinda stocked my answer is still reputable eventhough it's from 2015 haha – Tomblarom Aug 07 '22 at 11:32
  • I think this is the good answer self explaining when to use backgound and when to use backgroundTint. – Noman Shaikh Aug 13 '22 at 18:34
  • 3
    broken link.... – asdjfiasd Oct 25 '22 at 14:24
  • Not just a broken link, but hackable, putting those who click it at risk – LabGecko Aug 02 '23 at 12:31
  • Tried to edit the answer to fix the link, but got "Too many edits pending. Try again later." So, here's a (probably) useful link on buttons. Don't forget to look at the Material and Compose suggestions. https://developer.android.com/develop/ui/views/components/button – LabGecko Aug 02 '23 at 12:39
8

Many great methods presented above - One newer note

It appears that there was a bug in earlier versions of Material that prevented certain types of overriding the button color.

See: [Button] android:background not working #889

I am using today Material 1.3.0. I just followed the direction of KavinduDissanayake in the linked post and used this format:

app:backgroundTint="@color/purple_700"

(I changed the chosen color to my own theme of course.) This solution worked very simply for me.

zFreeindeed
  • 81
  • 1
  • 2
7

If the first solution doesn't work try this:

android:backgroundTint="@android:color/white"

I hope this work. Happy coding.

Pedro Molina
  • 1,101
  • 12
  • 12
5

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="http://schemas.android.com/apk/res/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 -->
</selector>

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"?>
<resources>
    <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>
</resources>

Screenshots below

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

Mikhael Lauda
  • 71
  • 1
  • 4
4

Starting with API 23, you can do:

btn.setBackgroundColor(getResources().getColor(R.color.colorOffWhite));

and your colors.xml must contain:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorOffWhite">#80ffffff</color>
</resources>
Zoe
  • 27,060
  • 21
  • 118
  • 148
4

Best way to change button color without losing button ghosting and other features.

Try it and you will see it is the best

app:backgroundTint="@color/color_name"
bzgd
  • 49
  • 3
2

in theme change this: parent="Theme.MaterialComponents.DayNight.DarkActionBar" to that: parent="Theme.AppCompat.Light.NoActionBar"

It worked for me after many search

2

usually with API 21 and above : just PUT this attribute : android:backgroundTint=" your color "

2

i'm using api 32 and I had to do it this way in the xml code:

android:backgroundTint="@android:color/white"
1

You can change the value in the XML like this:

<Button
    android:background="#FFFFFF"
     ../>

Here, you can add any other color, from the resources or hex.

Similarly, you can also change these values form the code like this:

demoButton.setBackgroundColor(Color.WHITE);

Another easy way is to make a drawable, customize the corners and shape according to your preference and set the background color and stroke of the drawable. For eg.

button_background.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="2dp" android:color="#ff207d94" />
    <corners android:radius="5dp" />
    <solid android:color="#FFFFFF" />
</shape>

And then set this shape as the background of your button.

<Button
    android:background="@drawable/button_background.xml"
     ../>

Hope this helps, good luck!

Akanshi Srivastava
  • 1,160
  • 13
  • 24
1

see the image and easy understand

image use

STA
  • 30,729
  • 8
  • 45
  • 59
Nurul
  • 89
  • 1
  • 1
1
  1. in new update of android studio you have to change the

button -> androidx.appcompat.widget.AppCompatButton

then only the button color will changed

res/drawable/button_color_border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#FFDA8200" />

    <stroke
        android:width="3dp"
        android:color="#FFFF4917" />

</shape>


And add button to your XML activity layout and set background android:background="@drawable/button_color_border".

<androidx.appcompat.widget.AppCompatButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_border"
    android:text="Button" />

Sumed
  • 317
  • 2
  • 13
1

Use below code for button background color

btn.setBackgroundResource(R.drawable.btn_rounded);

here is drawable xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/gray_scale_color"/>
            <corners android:radius="@dimen/_12sdp"/>

            <stroke android:width="0.5dp"
                android:color="?attr/appbackgroundColor"/>
        </shape>
    </item>
</layer-list>
Anshul Nema
  • 281
  • 3
  • 3
0

If you are trying to set the background as some other resource file in your drawable folder, say, a custom-button.xml, then try this:

button_name.setBackgroundResource(R.drawable.custom_button_file_name);

eg. Say, you have a custom-button.xml file. Then,

button_name.setBackgroundResource(R.drawable.custom_button);

Will set the button background as the custom-button.xml file.

0

I have the same problem the solution for me was the background color was colorPrimary of my theme you can use custom theme as one answer say above and set the colorPrimary to what you want

1- add this to your "value/themes/themes.xml" inside resources

<resources>
   <style name="Button.color" parent="ThemeOverlay.AppCompat">
       <item name="colorPrimary">@color/purple_500</item>
   </style>
</resources>

2- add this line to the button you want to have the color

    <Button
  
      android:theme="@style/Button.color"/>
Gmgm
  • 1
  • 1
0

Go to res > values > themes > theme.xml/themes.xml. Then change:

<style name="Theme.BirthdayGreet" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

To:

<style name="Theme.MemeShare" parent="Theme.AppCompat.Light.NoActionBar">)>

Watch this video for more information.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/29979636) – Boken Oct 02 '21 at 22:36
  • @Boken: This is clearly more than just a _link_ to an answer. It also includes specific guidance. – Jeremy Caney Oct 03 '21 at 00:26
0

backgroundTint above API21 background has no effect it takes colorPrimary of the theme by default

tintin
  • 335
  • 2
  • 8
0

Button background color in xml

<Button
    android:id="@+id/button"
    android:background="#0000FF"
    android:textColor="#FFFFFF"/>

Change button background color programmatically

Button button = findViewById(R.id.button);
button.setBackgroundColor(Color.BLUE);

Custom button background

shape.xml [res --> drawble]

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp" />
    <gradient
        android:angle="0"
        android:endColor="#0000FF"
        android:startColor="#00FFFF" />
    <stroke
        android:width="1dp"
        android:color="#000000" />
</shape>

Add this line

android:background="@drawable/shape"

full code

<Button
    android:id="@+id/button"
    android:background="@drawable/shape"
    android:textColor="#FFFFFF"/>

Material Design Button

Change button background color

app:backgroundTint="#0000FF"

Button Stroke color

app:strokeColor="#000000"

Button Stroke width

app:strokeWidth="2dp"

Full code

<Button
    android:id="@+id/button"
    android:textColor="#FFFFFF"
    app:backgroundTint="#0000FF"
    app:strokeColor="#000000"
    app:strokeWidth="2dp"/>

Hope you helpful!

0

Go to themes.xml file under res -> values -> themes

Go to the line -

<style name="Theme.YourProjectName" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

and change it to -

<style name="Theme.YourProjectName" parent="Theme.AppCompat.DayNight.DarkActionBar">

Then proceed with changing

android:backgroundTint to desired color

Payel Senapati
  • 1,134
  • 1
  • 11
  • 27
0

Buttons use colorPrimary attribute by default.

If you are styling using Theme:

<item name="colorPrimary">@color/yourColor</item> 

Note that other widgets also use this attribute.

Drop any other widgets, that you know use this attribute, in the comments.

Osagui Aghedo
  • 330
  • 4
  • 12
0

btn_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape  android:shape="rectangle" >
            <corners android:radius="10dp" />
        </shape>
    </item>
</selector>

btn_color_state.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--    state pressed-->
    <item android:color="#94E4A2" android:state_pressed="true"/>
    <!--    state Disabled-->
    <item android:color="#c5c5c5"  android:state_enabled="false"/>
    <!--    state default-->
    <item android:color="#36df54" />
</selector>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:orientation="vertical"
    android:weightSum="2"
    tools:context=".MainActivity">


    <Button
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:text="Hello World"
        android:background="@drawable/btn_bg"
        app:backgroundTint="@color/btn_color_state"
        android:layout_margin="8dp" />

    <Button
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:text="Hello World"
        android:enabled="false"
        android:background="@drawable/btn_bg"
        app:backgroundTint="@color/btn_color_state"
        android:layout_margin="8dp" />
</LinearLayout>

Meet Bhavsar
  • 442
  • 6
  • 12
-2

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.
b1.setBackgroundResource(R.color.colorAccent);  
SHS
  • 1,414
  • 4
  • 26
  • 43