I want to make an if statement in which I will compare the color of my layout background color, something like this:
RelativeLayout screen = (RelativeLayout) findViewById (R.id.screen);
if(screen.getBackgroundColor() == "#FFFFFFFF"){
//do something...
}
else{
//do something else...
}
I can’t find out how to get the background color. I am setting the color by: screen.setBackgroundColor(Color.parseColor("#000000"));
There’s no direct
getBackgroundColor(), but if you really need to know the color value, you should retrieve view’s Paint object and get color from it:Note this contains alpha as well, so be careful with your comparison (or get rid of alpha if you just need to compare RGB:
This will work on all API levels.
EDIT
As for the comparision – once you got color retrieved you can use Color.parseColor() and compare the result usual way:
or same with ARGB:
And if you are doing this frequently (like in. list adapter) I’d parse it once and store the results for reuse.