Rick
Rick

Reputation: 4013

Enable touch feedback for every buttons

With Android Studio I have some Buttons and I want that when I click on them you can see a kind of movement that confirm you have clicked that Button. Is there a way to do that without create a new xml file in which there is a selector? I'd like to do something like that (I think it's default color):

enter image description here when is normal

enter image description here when is pressed

I have this but It doesn't do anything:

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;
        }
    });

Upvotes: 5

Views: 3795

Answers (1)

Prachur
Prachur

Reputation: 1110

I think you are looking for something like this.

OnTouch . You can capture key down and key up events to change the color.

You can try this :-

        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;
        }
    });

Upvotes: 6

Related Questions