0

Hello I'm a newbie in android development and I am looking forward a way to change the background color.

Here is my code:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// View layout = new View(this);
// layout.setBackgroundColor(android.R.color.holo_blue_light);
// View root = layout.getRootView();
// root.setBackgroundColor(android.R.color.holo_green_dark);
View view = this.getWindow().getDecorView();
view.setBackgroundColor(R.color.red); 
3
  • Use setBackgroundResource() instead setBackgroundColor() Commented May 20, 2013 at 13:40
  • or change the background in the layout file Commented May 20, 2013 at 13:40
  • possible duplicate of How to set background color of a View Commented May 20, 2013 at 13:41

3 Answers 3

2

The problem is with the syntax view.setBackgroundColor(R.color.red);
Try:

view.setBackgroundColor(Color.RED); 
Sign up to request clarification or add additional context in comments.

1 Comment

or getResources.getColor(R.color.Red)
1

view.setBackgroundResource(R.drawable.example);

Comments

0

used this :-

 view.setBackgroundColor(ContextCompat.getColor(context, R.color.color_name))

This will choose the Marshmallow two parameter method or the pre-Marshmallow method appropriately.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.