Enable touch feedback for each button

C Android StudioI have several Buttons, and I want you to see some movement when you click on them, confirming that you clicked on it Button. Is there a way to do this without creating a new file xmlin which there is selector? I would like to do something like this (I think this is the default color):

enter image description here when normal

enter image description here when you press

I have this, but it does nothing:

Button b = (Button)findViewById(R.id.btn);
    b.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                return true;
            }

            return false;
        }
    });
+4
source share
1 answer

I think you are looking for something like this.

OnTouch . To change color, you can record key events and up keys.

You can try the following: -

        final Button button = (Button) findViewById(R.id.button1);

    button.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                button.setBackgroundColor(Color.RED);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {

                button.setBackgroundColor(Color.GREEN);
                return true;
            }
            return false;
        }
    });
+5
source

All Articles