xml which is under the values folder,then you should call the following: root. setBackgroundColor(getResources(). getColor(R.color.name));
Programmatically using Java code. java file, then you can do it by using setBackgroundColor() method on your layout. To your Parent View Layout add an attribute @id/id_name and map it to a variable in your java file.
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"));
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));
You can use
root.setBackgroundColor(0xFFFFFFFF);
or
root.setBackgroundColor(Color.parseColor("#ffffff"));
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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With