179

I try to set background color programmatically but when I set every one of my colors, the background being black but with any color background being white like the application theme.

View someView = findViewById(R.id.screen);
View root = someView.getRootView();
root.setBackgroundColor(color.white);

Can you see the code?

Pankaj Lilan
  • 4,245
  • 1
  • 29
  • 48
user3274646
  • 1,901
  • 2
  • 13
  • 7

8 Answers8

234

I didn't understand your question ... what do you mean by "when i set every one of my colour"? try this (edit: "#fffff" in original answer changed to "#ffffff"

  yourView.setBackgroundColor(Color.parseColor("#ffffff"));
hBrent
  • 1,696
  • 1
  • 17
  • 38
M.Khouli
  • 3,992
  • 1
  • 23
  • 26
145

you need to use getResources() method, try to use following code

View someView = findViewById(R.id.screen);
View root = someView.getRootView();
root.setBackgroundColor(getResources().getColor(color.white)); 

Edit::

getResources.getColor() is deprecated so, use like below

 root.setBackgroundColor(ContextCompat.getColor(this, R.color.white)); 
King of Masses
  • 18,405
  • 4
  • 60
  • 77
Sainath Patwary karnate
  • 3,165
  • 1
  • 16
  • 18
46

You can use

 root.setBackgroundColor(0xFFFFFFFF);

or

 root.setBackgroundColor(Color.parseColor("#ffffff"));
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • 1
    root.setBackgroundColor(Color.RED); also – Zar E Ahmer Jan 13 '15 at 10:54
  • 1
    If using the first example, which bytes map to which color components? For instance, is it `0xRRGGBBAA`, or `0xAARRGGBB`, or maybe `0xBBGGRRAA` or perhaps `0xXXRRGGBB`? Doesn't really matter if you're doing white and black, but for basically any other color it's necessary to know this information. – aroth Aug 19 '16 at 05:25
  • 1
    @Aroth `0xAARRGGBB`. – Lorne Laliberte Sep 27 '16 at 20:21
37

The previous answers are now deprecated, you need to use ContextCompat.getColor to retrieve the color properly:

root.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));
Yoann Hercouet
  • 17,894
  • 5
  • 58
  • 85
  • 2
    in kotlin root.setBackgroundColor(ContextCompat.getColor(activity?.applicationContext!!, R.color.white)); – TapulaRasa Jul 07 '21 at 14:40
28

If you just want to use some of the predefined Android colors, you can use Color.COLOR (where COLOR is BLACK, WHITE, RED, etc.):

myView.setBackgroundColor(Color.GREEN);

Otherwise you can do as others have suggested with

myView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.myCustomGreen));

I don't recommend using a hex color directly. You should keep all of your custom colors in colors.xml.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
20

This must work:

you must use getResources().getColor(R.color.WHITE) to get the color resource, which you must add in the colors.xml resource file

View someView = findViewById(R.id.screen);

someView.setBackgroundColor(getResources().getColor(R.color.WHITE));
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Hatim
  • 1,516
  • 1
  • 18
  • 30
2

If you save color code in the colors.xml which is under the values folder,then you should call the following:

root.setBackgroundColor(getResources().getColor(R.color.name));

name means you declare in the <color/> tag.

Pugazh
  • 9,453
  • 5
  • 33
  • 54
Hay Thi
  • 155
  • 1
  • 4
2

In my case it wasn't changing the color because I was setting the color in my xml resource.

After delete the line that set the color it worked perfectly programmatically

This is an example I did in a RecyclerView

final Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_icon).mutate();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    holder.image.setBackground(drawable);
} else {
    holder.image.setBackgroundDrawable(drawable);
}
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97