I am designing a web view app on Android Studio. I have a Navigation drawer and I have been successful in making the colour change of a an item which is selected. What I want right now is to be able to assign different colours to different items when selected. For example if the user selects 1st item, the background color of that item becomes orange but if the user selects second item the background of the 2nd item changes to blue.
Asked
Active
Viewed 293 times
1 Answers
0
You get that view and set its background color to the required color when its onTouch has been activated.
Example:
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
view.setBackground(<yourColorToChangeTo>);
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED
view.setBackground(<originalColor>);
return true; // if you want to handle the touch event
}
return false;
}
});
return true;
};
UPDATE
Do this to keep the color once clicked:
<yourView>.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
view.setBackground(<ColorToChangeTo>);
}
});

TejjD
- 2,571
- 1
- 17
- 37
-
I would want the color to stay until the next button is clicked. How to do that? – angad singh Dec 31 '15 at 09:10
-
I have updated the answer. That should solve your question. – TejjD Dec 31 '15 at 09:14
-
1Worked like a charm. Thanks a lot for the help. – angad singh Dec 31 '15 at 09:44