Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reseting the background color of a View

I'm trying to restore the background Color of a View.

I have several selectable Views. When the user clicks one of those Views, the following code is executed and the View becomes Yellow:

View newSelection, previousSelection;

...

if(previousSelection != null) {
    previousSelection.setBackgroundColor(Color.BLACK); // problem here
}
newSelection.setBackgroundColor(Color.YELLOW);

However, I want to reset the color of the previously selected View. However, I do not know which color it was (I'm setting it to Color.BLACK in the above code). I was not able to find a getBackgroundColor or similar method in the View class. If I had it, I could save the previous color and just put it back when the new View is selected.

like image 656
MyName Avatar asked Sep 08 '10 19:09

MyName


1 Answers

use View.getBackground(), it returns the current 'Drawable' background of the view which can then be used in View.setBackgroundDrawable()

View theView;
Drawable originalBackground;

...

originalBackground = theView.getBackground();

theView.setBackgroundColor(Color.YELLOW);

...
theView.setBackgroundDrawable(originalBackground);
like image 110
mmlc31 Avatar answered Sep 18 '22 17:09

mmlc31