Eternal Learner
Eternal Learner

Reputation: 2632

Why doesn't R.color.white work where Color.WHITE does?

I'm trying to set the background of a very simple layout:

  LinearLayout ll = (LinearLayout) findViewById(R.id.simple_layout);

This works:

  ll.setBackgroundColor(Color.WHITE);

But this doesn't:

  ll.setBackgroundColor(R.color.white);

And yes, I verified that R.color.white is defined in a colors.xml file under /res/values.

What am I missing?

Upvotes: 3

Views: 4232

Answers (2)

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28359

As @Ali points out when you have it set in XML it becomes a resource. This is very confusing obviously because you don't think of it as a resource, you think of it as a value. But just like all the other resources, it is a resource. So yes, you have to use setBackgroundResource()

Upvotes: 1

Ali
Ali

Reputation: 9994

You need to use ll.setBackgroundResource(R.color.white)

Upvotes: 7

Related Questions