Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
        }
    });
like image 985
Rick Avatar asked Dec 23 '13 23:12

Rick


Video Answer


1 Answers

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;
        }
    });
like image 82
Prachur Avatar answered Sep 24 '22 08:09

Prachur