Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dragging and pressing buttons - android

I have several buttons and I would like to press on one of them and drag through another presing them. Could you tell me which MotionEvent or another functionality should I use. I'm using onTouchListener.

There is an image where you can see what I want to do (first ACTION_DOWN on 1st button and drag through 2nd-7th buttons still pressing the screen) and finally press every white buttons:

enter image description here

Below is my onTouch button code:

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

        button1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                    switch (event.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:
                            soundIDs[0] = sound.play(R.raw.sample1);
                            button1.setBackgroundResource(R.drawable.white_clicked);
                            break;
                        case MotionEvent.ACTION_UP:
                        case MotionEvent.ACTION_CANCEL:
                            sound.stop(soundIDs[0]);
                            button1.setBackgroundResource(R.drawable.white);
                            break;
                    }
            return false;
            }
        });
like image 314
CookieMonssster Avatar asked Dec 20 '25 13:12

CookieMonssster


1 Answers

You are setting the OnTouchListener on just the one button. That's not going to help you know when the pointer moves (e.g. user drags his finger) into another button.

You could set an OnTouchListener on the view that contains the buttons. Then check for ACTION_DOWN, ACTION_MOVE, and ACTION_UP events. You would then have to do some simple hit detection to figure out which button to activate.

Something along the lines of:

getWindow().getDecorView()
    .findViewById(android.R.id.content)
    .setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            int action = event.getAction();
            if (action != MotionEvent.ACTION_DOWN 
                && action != MotionEvent.ACTION_MOVE 
                && action != MotionEvent.ACTION_UP) return false;

            Rect hitRect = new Rect();
            Button button;
            for(int i = 0; i < myButtons.size(); i++) {
                button = myButtons.get(i);
                button.getHitRect(hitRect);
                if (hitRect.contains((int)event.getX(), (int)event.getY())) {
                    button.setText("hit!");
                }
            }
            return true;
        }

    });

Where myButtons is an ArrayList of your buttons (piano keys in your example).

Also, you'd probably want to modify this to properly deactivate the currently active button if the user's touch leaves the button, but doesn't hit another button.

I tested the above code on an android device with a layout that has 3 buttons in a row. Dragging your finger across all the buttons causes each button's text to change to "hit!"

Like I said above, you were setting the touch listener on the just one button, that will not work. In this example I have set the touch listener on the entire view for the activity.

like image 185
Brett Duncavage Avatar answered Dec 22 '25 22:12

Brett Duncavage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!