How to set background color of a View

The question:

I’m trying to set the background color of a View (in this case a Button).

I use this code:

// set the background to green
v.setBackgroundColor(0x0000FF00 );

It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?


The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You made your button transparent. The first byte is the alpha.

Try v.setBackgroundColor(0xFF00FF00);

Method 2

When you call setBackgoundColor it overwrites/removes any existing background resource, including any borders, corners, padding, etc.
What you want to do is change the color of the existing background resource…

View v;
v.getBackground().setColorFilter(Color.parseColor("#00ff00"), PorterDuff.Mode.DARKEN);

Experiment with PorterDuff.Mode.* for different effects.

Method 3

Several choices to do this…

Set background to green:


Set background to green with Alpha:


Set background to green with Color.GREEN constant:


Set background to green defining in Colors.xml

<?xml version="1.0" encoding="utf-8"?>
    <color name="myGreen">#00FF00</color> 
    <color name="myGreenWithAlpha">#FF00FF00</color> 

and using:




or the longer winded:

v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreen));


v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreenWithAlpha));

Method 4

You can set the hex-color to any resource with:


Method 5

// set the background to green
v.setBackgroundColor(0x0000FF00 );

The code does not set the button to green. Instead, it makes the button totally invisible.

Explanation: the hex value of the color is wrong. With an Alpha value of zero, the color will be invisible.

The correct hex value is 0xFF00FF00 for full opacity green. Any Alpha value between 00 and FF would cause transparency.

Method 6

For setting the first color to be seen on screen, you can also do it in the relevant layout.xml (better design) by adding this property to the relevant View:


Method 7

and what is the correct way to change
the background color on any View?

On any View? What you have is correct, though you should drop the invalidate() call.

However, some Views already have backgrounds. A Button, for example, already has a background: the face of the button itself. This background is a StateListDrawable, which you can find in android-2.1/data/res/drawable/btn_default.xml in your Android SDK installation. That, in turn, refers to a bunch of nine-patch bitmap images, available in multiple densities. You would need to clone and modify all of that to accomplish your green goals.

In short, you will be better served finding another UI pattern rather than attempting to change the background of a Button.

Method 8

try to add:


Method 9

I use at API min 16 , target 23

Button WeekDoneButton = (Button) viewWeeklyTimetable.findViewById(;

WeekDoneButton.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));

Method 10


Method 11

You can simple use :


Method 12

This question talks about changing the background color of a view. In one of the answers, the person explains how to change the background color during runtime. Obviously you are going to look into how to modify other objects on the screen, but this should give you a good start by at least allowing you to modify the background color of the view on button click.

Method 13

Stating with Android 6 use ContextCompact

        view.setBackgroundColor( ContextCompat.getColor(this, R.color.your_color));

Method 14

You can simple use :

view.setBackgroundColor(Color.rgb(0, 198, 255));

Method 15

This works for me


That way only changes the color of the background without change the background itself. This is usefull for example if you have a background with rounded corners.

Method 16

In kotlin you could do it like this:

val backgroundColor = R.color.whatever_color_you_like

Where getColorCompat() is an extension function:

 * Extension method to provide simpler access to {@link ContextCompat#getColor(int)}.

 fun Context.getColorCompat(color: Int) = ContextCompat.getColor(this, color)

Method 17


Adds color to previous color value, so i have a different color.

What works for me is :


Method 18

Let suppose we have a primary color in values=>colors.xml as:

    <color name="primary">#FDD835</color>

so if we want to use our custom color into setBackgroundColor(@ColorInt int Color) then we just need an annotation @SuppressLint("ResourceAsColor") with constructor/method which will be used as:

    public _LinearLayout(Context context) {

        // Formatting our layout : )



Method 19

You must pass an int in the argument.

First Example:


Second Example:

int colorId =;


Method 20

This should work fine: v.setBackgroundColor(0xFF00FF00);

Method 21

I tried all the above ways. But I havent achieve what i need. Here is my try.
If you are using hexcode for color and want to set the color as background of image, then this is the kotlin code.

val bitmap = Bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val colorCode = "#ffffff"

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment