Set the background color programmatically

I am trying to set the background color programmatically, but when I set each of my colors, the background is black, but with any color background, white as the theme of the application.

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

Do you see the code?

+64
android background-color
May 7 '14 at 12:29
source share
8 answers

I did not understand your question ... what do you mean by "when I set my every color"? try this (change: "#fffff" in the original answer changed to "#ffffff"

  yourView.setBackgroundColor(Color.parseColor("#ffffff")); 
+99
May 7 '14 at 12:43
source share

you need to use getResources () method, try using the 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 as below

  root.setBackgroundColor(ContextCompat.getColor(this, R.color.white)); 
+47
May 7, '14 at 12:45
source share

you can use

  root.setBackgroundColor(0xFFFFFFFF); 

or

  root.setBackgroundColor(Color.parseColor("#ffffff")); 
+31
May 7 '14 at 12:41
source share

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

 root.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white)); 
+23
Apr 15 '16 at 16:15
source share

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,

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

I do not recommend using hexagonal color directly. You must save all your custom colors in colors.xml.

+5
Jan 06 '17 at 5:28
source share

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

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

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

+2
Jun 24 '16 at 9:21
source share

this should work: you should use getResources (). getColor (R.color.WHITE) to get the color resource that you must add in the colors.xml resource file

 View someView = findViewById(R.id.screen); someView.setBackgroundColor(getResources().getColor(R.color.WHITE)); 
+2
Nov 09 '16 at 14:46
source share

In my case, this did not change color, because I was color setting in my xml resource .

After removing the line that sets the color, it worked perfectly programmatically

This is an example I made in 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); } 
+1
Oct 11 '16 at 15:53
source share



All Articles