Android Detect When Finger Is Hovering over a Button
Asked Answered
G

0

6

I have created a game where if the user presses a button, it moves the snake in that particular direction.

Rather then when you PRESS a button, I want to make it so when the finger is hovering over the button the snake moves instead.

So I have changed my methods from setOnTouchListener, to setOnHoverListener

here is my code:

Button btnRight = (Button) findViewById(R.id.btnRight);
btnRight.setOnHoverListener(new OnHoverListener(){
    public boolean onHover(View v, MotionEvent event) {
        if(direction!=4)
            direction = 6;
        return false;
    }
});

When the finger is pressed on the screen and gets dragged over the button, nothing happens.

How would I go about having it register when the finger is dragged over the button? Is there an easy method solution or am I gonna have to detect coordinates, etc?

Groundless answered 15/1, 2013 at 20:30 Comment(4)
I don't think that commonly used touch displays can tell you that you are just 'hovering' your finger.Negotiate
Yes, you have to check the coordinates where the finger is pressed, and compare them to the coordinates of the button.Kevon
On re-read I see what you're trying to do. You want to let someone touch the screen anywhere, drag their finger over the button, and then register that their finger is over the button without required a new click (i.e. they don't have to touch the button directly)Pronto
just so don't waste time with it, the onHover method is reserved for pointer capable devices (i.e. mouse, stylus, ...)Impropriety

© 2022 - 2024 — McMap. All rights reserved.