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"/> 2 19 Answers
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.
2You 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); 3 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
If the first solution doesn't work try this:
android:backgroundTint="@android:color/white"I hope this work. Happy coding.
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.
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
- Go to res
- Expand it, right click on drawable
- New -> Drawable Resource File
- 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 -->
</selector>Second, go to res
- Expand values
- Double click on colors.xml
- 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
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" 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> 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=""> <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!
see the image and easy understand
2- 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="" 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 backgroundandroid: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" /> usually with API 21 and above : just PUT this attribute : android:backgroundTint=" your color "
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.
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"/> 1 in theme change this: parent="Theme.MaterialComponents.DayNight.DarkActionBar" to that: parent="Theme.AppCompat.Light.NoActionBar"
It worked for me after many search
backgroundTint above API21 background has no effect it takes colorPrimary of the theme by default
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=""> <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!
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.
2To 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);