onHoverListener doesn't work in Android
19,692
Solution 1
Hovering requires support from the hardware. The only thing likely to support it is a stylus. It won't work with just your finger.
Solution 2
try using OnFocusChangeListener()
.
PS worked for me
Author by
Admin
Updated on July 25, 2022Comments
-
Admin almost 2 years
In android document, it mentions supporting the "hover" event since 4.0 (ie. API level 14 and up). But somehow, it doesn't work. Even I tried out the sample code in ApiDemo, which is from Android Sample, it didn't work. My current device is Android 4.0.4. Should I upgrade it to 4.2.2?
Sample code is something as below. Did you have a solution to it? Thanks a lot.
Code:
View container = findViewById(R.id.container); container.setOnHoverListener(new View.OnHoverListener() { @Override public boolean onHover(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_HOVER_ENTER: mMessageTextView.setText(Hover.this.getResources().getString( R.string.hover_message_entered_at, event.getX(), event.getY())); break; case MotionEvent.ACTION_HOVER_MOVE: mMessageTextView.setText(Hover.this.getResources().getString( R.string.hover_message_moved_at, event.getX(), event.getY())); break; case MotionEvent.ACTION_HOVER_EXIT: mMessageTextView.setText(Hover.this.getResources().getString( R.string.hover_message_exited_at, event.getX(), event.getY())); break; } return false; } });
-
Admin over 10 yearsThanks. Will it work with mouse? e.g. a bluetooth mouse compatible with Android device?
-
Gabe Sechan over 10 yearsNot sure, I've never tried it. I think it might (a non-clicked mouse as a hover makes sense), but I can't swear it will.
-
Gabe Sechan over 10 yearsAs an update to this- some devices will now be able to detect a single finger hovering. But really only the higher end devices, like the Note. You can't rely on it.
-
Amit Hooda over 7 yearsI tried the code on android emulator, it didn't work on that using mouse. Have someone else tried and got it working ?