How to change the color of a button?

AndroidAndroid LayoutAndroid Button

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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="@android:color/white"

Coding:

//btn represents your button object

btn.setBackgroundColor(Color.WHITE);
btn.setTextColor(Color.BLACK);

Solution 3 - Android

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>"

Note: If you're going to work with android/java you really should learn how to google ;)
How to customize different buttons in Android

Solution 4 - Android

If the first solution doesn't work try this:

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

I hope this work. Happy coding.

Solution 5 - Android

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.

Solution 6 - 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="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

Solution 7 - Android

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>

Solution 8 - Android

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"

Solution 9 - Android

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!

Solution 10 - Android

see the image and easy understand

image use

Solution 11 - Android

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

Solution 12 - Android

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.

Solution 13 - Android

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

Solution 14 - Android

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

It worked for me after many search

Solution 15 - Android

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

Solution 16 - Android

> 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!

Solution 17 - Android

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.

Solution 18 - Android

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);  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhahoanghai.uehView Question on Stackoverflow
Solution 1 - AndroidNechemia HoffmannView Answer on Stackoverflow
Solution 2 - AndroidAicha HezitView Answer on Stackoverflow
Solution 3 - AndroidTomblaromView Answer on Stackoverflow
Solution 4 - AndroidPedro MolinaView Answer on Stackoverflow
Solution 5 - AndroidzFreeindeedView Answer on Stackoverflow
Solution 6 - AndroidMikhael LaudaView Answer on Stackoverflow
Solution 7 - Androidsachin pathakView Answer on Stackoverflow
Solution 8 - AndroidbzgdView Answer on Stackoverflow
Solution 9 - AndroidAkanshi SrivastavaView Answer on Stackoverflow
Solution 10 - AndroidNurulView Answer on Stackoverflow
Solution 11 - AndroidSumedView Answer on Stackoverflow
Solution 12 - AndroidPranav PrakasanView Answer on Stackoverflow
Solution 13 - AndroidGmgmView Answer on Stackoverflow
Solution 14 - AndroidAbbas FarajiView Answer on Stackoverflow
Solution 15 - AndroidtintinView Answer on Stackoverflow
Solution 16 - AndroidAdeeksha SathsaraView Answer on Stackoverflow
Solution 17 - AndroidAnshu kumarView Answer on Stackoverflow
Solution 18 - AndroidManika AgrawalView Answer on Stackoverflow