onHoverListener doesn't work in Android
Asked Answered
S

5

9

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;
        }
    });

Spectrometer answered 28/7, 2013 at 14:56 Comment(0)
P
10

Hovering requires support from the hardware. The only thing likely to support it is a stylus. It won't work with just your finger.

Pacifist answered 28/7, 2013 at 15:0 Comment(4)
Thanks. Will it work with mouse? e.g. a bluetooth mouse compatible with Android device?Spectrometer
Not 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.Pacifist
As 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.Pacifist
I tried the code on android emulator, it didn't work on that using mouse. Have someone else tried and got it working ?Maryalice
W
1

try using OnFocusChangeListener(). PS worked for me

Welch answered 8/3, 2016 at 11:53 Comment(0)
A
0

I suggest to Turn on 'Accessibility' and 'Explore by touch' feature in settings. When these features are off, the hover action will be treated as touch actions.

Appassionato answered 8/10, 2013 at 5:41 Comment(0)
M
0

setOnTouchListener is also an option. Here is a kotlin example:

    binding.ratingBar.setOnTouchListener { v, event ->
        println("touch......")
        false
    }

here (binding.ratingBar) is my view reference. I used view binding.

Mesosphere answered 25/7, 2021 at 9:21 Comment(0)
T
0

Try to debug on a Chromebook, Alternatively, If you do not have one like me, Install fydeOS over a VMware station. Enable debugging mode on fydeOS so you can debug your app using android studio. If you do everything right, You will see the fydeOS device on the android studio device manager list.

Note: fydeOS has separated image for WMware and supported hardwares. see here

enter image description here

Tenedos answered 14/2, 2023 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.