Tal Angel
Tal Angel

Reputation: 1782

Detecting hover/swipe over a button

I wish to create a simple game for Android, where the player will be shown a table of buttons. He should have the ability to swipe/drag his fingers over some buttons, and the result of his move should only be displayed after he stops touching the screen.

I can see when Action_UP and Action_Down is called, but I can't use Action_HOVER_MOVE:

View.OnTouchListener OTL = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {           

        if (motionEvent.getAction()==MotionEvent.ACTION_DOWN
                || motionEvent.getAction()==MotionEvent.ACTION_HOVER_MOVE)

        {
            Button B = (Button)view;
            if (B.isActivated())
            {
                String letter = B.getText()+"";
                word+=letter;
            }
            B.setActivated(false);
            Toast t = Toast.makeText(MainActivity.this,word,Toast.LENGTH_SHORT);
            t.show();

        }
        if (motionEvent.getAction()==MotionEvent.ACTION_UP)
        {
           Toast t = Toast.makeText(MainActivity.this,word,Toast.LENGTH_SHORT);
            t.show();
            word="";
            resetButtons();
        }

        return true;
    }
};

How to detect swipe over other buttons?

Upvotes: 1

Views: 2210

Answers (1)

Glen Pierce
Glen Pierce

Reputation: 4801

Per the docs, you probably want ACTION_MOVE

int ACTION_MOVE
Constant for getActionMasked(): A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP). The motion contains the most recent point, as well as any intermediate points since the last down or move event.

You're having trouble with ACTION_HOVER_MOVE because:

int ACTION_HOVER_MOVE
Constant for getActionMasked(): A change happened but the pointer is not down (unlike ACTION_MOVE). The motion contains the most recent point, as well as any intermediate points since the last hover move event.

This action is always delivered to the window or view under the pointer.

This action is not a touch event so it is delivered to onGenericMotionEvent(MotionEvent) rather than onTouchEvent(MotionEvent).

Source: https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_MOVE

Upvotes: 2

Related Questions