0

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.

1 Answers1

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