Set background color of TableRow
Asked Answered
S

5

5

I try to set the background color of a TableRow. Currently I have this in my XML file:

android:background="@color/buttonBackground" 

and i work great. But when it run

row.setBackgroundColor(R.color.red);

the row disappears. Can someone explain why that is?

Surfboat answered 20/5, 2011 at 20:44 Comment(0)
F
6

I believe you need to do:

Resources resource = context.getResources();
row.setBackgroundColor(resource.getColor(R.color.red)
Frenchpolish answered 20/5, 2011 at 20:48 Comment(1)
getColor() deprecated New; row.setBackgroundColor(ContextCompat.getColor(this,R.color.colorAccent))Vedis
L
3

You can also try :

row.setBackgroundColor(Color.RED);

This works for me in Android Studio 2.2.3.

Lilylivered answered 9/3, 2017 at 8:18 Comment(0)
L
2

You must be missing the alpha value in your color definition. Verify it has 4 bytes, like #FFFFFFFF.

Leventhal answered 20/5, 2011 at 20:50 Comment(0)
I
0
row.setBackgroundColor(ContextCompat.getColor(this,R.color.colorAccent))
Intoxicate answered 7/7, 2020 at 7:30 Comment(0)
C
-2

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

Chanteuse answered 11/11, 2013 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.