How to change the color of a button?
Asked Answered
B

25

86

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"/>
Bandore answered 19/9, 2015 at 17:10 Comment(1)
@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.Kenon
K
52

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);
Kulda answered 20/9, 2015 at 4:25 Comment(3)
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.Detergent
don't use this - you'll lose the button animation (lift and ripple)Romaineromains
@SomeoneSomewhere that's right. See the correct answer.Newcomer
N
109

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.

Newcomer answered 2/5, 2019 at 14:27 Comment(6)
The backgroundTint is the way to go.Webbed
For some reason, both methods do not work for me. Although I can see it from the Design tab, when I run the app, it still shows me the default color. (I also tried android:background but did not work either.)Daberath
theme doesn't work, only backgroundTint worksLozenge
@DavidLee did you found out what was the issue?Cachexia
@CoryRoy Why do you believe backgroundTint is superior?Convict
@Convict backgroundTint multiplies rather than replaces, so shadows etc won't be removed but still work with the new colour.Webbed
K
52

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);
Kulda answered 20/9, 2015 at 4:25 Comment(3)
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.Detergent
don't use this - you'll lose the button animation (lift and ripple)Romaineromains
@SomeoneSomewhere that's right. See the correct answer.Newcomer
P
34

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

Paraphernalia answered 19/9, 2015 at 17:29 Comment(5)
@PayelSenapati kinda stocked my answer is still reputable eventhough it's from 2015 hahaParaphernalia
I think this is the good answer self explaining when to use backgound and when to use backgroundTint.Elbowroom
broken link....Hermaphroditus
Not just a broken link, but hackable, putting those who click it at riskConvict
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. developer.android.com/develop/ui/views/components/buttonConvict
B
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.

Bargello answered 25/5, 2021 at 3:46 Comment(0)
U
7

If the first solution doesn't work try this:

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

I hope this work. Happy coding.

Ugric answered 20/4, 2021 at 8:15 Comment(2)
This didn't work for me today. using app: instead of android: as suggested by @Bargello fixed itAtmometer
this code worked for me, its changes the background color of the buttonBelostok
S
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

Spittoon answered 29/7, 2018 at 4:55 Comment(0)
C
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>
Cowcatcher answered 12/1, 2019 at 21:1 Comment(0)
E
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"
Expropriate answered 7/8, 2021 at 0:44 Comment(0)
W
2

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

It worked for me after many search

Withershins answered 10/1, 2022 at 21:27 Comment(0)
D
2

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

Duro answered 29/5, 2022 at 20:0 Comment(0)
Y
2

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

android:backgroundTint="@android:color/white"
Yancey answered 30/10, 2022 at 15:39 Comment(0)
B
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!

Bicorn answered 24/6, 2020 at 10:51 Comment(0)
S
1

see the image and easy understand

image use

Superintendency answered 18/7, 2021 at 4:17 Comment(2)
Welcome to SO. It is better to code instead of sending a screenshotDarwinism
meta.#286051Miraculous
I
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" />

Ipomoea answered 10/2, 2022 at 11:19 Comment(0)
O
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>
Ostensive answered 8/8, 2022 at 5:15 Comment(0)
R
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.

Roughandready answered 24/6, 2020 at 10:34 Comment(0)
S
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"/>
Shing answered 20/7, 2021 at 13:24 Comment(1)
Please attach a code snippet or example so others can better understand your solution. Thanks!Slacks
E
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.

Excursus answered 2/10, 2021 at 22:20 Comment(2)
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 ReviewAristippus
@Boken: This is clearly more than just a link to an answer. It also includes specific guidance.Garner
S
0

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

Stonewall answered 28/1, 2022 at 9:40 Comment(0)
D
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!

Dissentient answered 11/2, 2022 at 14:53 Comment(0)
L
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

Lozenge answered 4/12, 2022 at 14:14 Comment(0)
L
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.

Low answered 23/2, 2023 at 10:10 Comment(0)
A
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>

Arcane answered 7/5, 2023 at 12:34 Comment(0)
E
0

I usually use this when I have a Shape to just change the background

btn.setBackgroundTintList(getResources().getColorStateList(R.color.yourcolor));

For me it works really well :)

Evacuation answered 28/6 at 12:55 Comment(0)
S
-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);  
Stpierre answered 30/6, 2018 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.