Set the background color of the TableRow

I am trying to set the background color of a TableRow. I currently have this in my xml file:

android:background="@color/buttonBackground" 

and I work great. But when it starts

row.setBackgroundColor(R.color.red);

the line disappears. Can someone explain why this is?

+3
source share
4 answers

I believe what you need to do:

Resources resource = context.getResources();
row.setBackgroundColor(resource.getColor(R.color.red)
+4
source

The color definition must not contain an alpha value. Make sure it has 4 bytes, for example #FFFFFFFF.

+2
source

You can also try:

row.setBackgroundColor(Color.RED);

This works for me in Android Studio 2.2.3.

+1
source

Just write: android:background="@android:color/buttonBackground"

-1
source

All Articles