5

Samsung Galaxy S4 have the "Floating Touch" functionality, in which the finger can be detected even if the screen is not touched.

I would like to fire an event on a button (btn1) when the finger is passing hover it.

I tried using the OnHoverListener, but onHover never get called when the MotionEvent is MotionEvent.ACTION_HOVER_ENTER or MotionEvent.ACTION_HOVER_EXIT or MotionEvent.ACTION_HOVER_MOVE, which are the event that I need.

This is my code:

btn1.setOnHoverListener(new OnHoverListener() {
@Override
public boolean onHover(View v, MotionEvent event) {
    Log.d("FloatingTouch", "onHover: "+event.getAction());
    switch (event.getAction()) {
      case MotionEvent.ACTION_HOVER_ENTER:
          Log.d("FloatingTouch", "ACTION_HOVER_ENTER" );
          return true;
      case MotionEvent.ACTION_HOVER_MOVE:
          Log.d("FloatingTouch", "ACTION_HOVER_MOVE" );
          return true;
      case MotionEvent.ACTION_HOVER_EXIT:
          Log.d("FloatingTouch", "ACTION_HOVER_EXIT" );
          break;
      default:
          break;
    }
    return false;
}
});

Do I miss something? Maybe some permission?

NG_
  • 6,895
  • 7
  • 45
  • 67
Nifhel
  • 2,013
  • 2
  • 26
  • 39

1 Answers1

4

OK, I finally got it!

To make it work you have to:

  1. Add a new intent filter to com.sec.android.airview.HOVER for your Activity in the manifest:
<intent-filter>
<action android:name="com.sec.android.airview.HOVER" />
</intent-filter>
  1. Add an onHoverListener to you View
convertView.setOnHoverListener(new OnHoverListener() {
    @Override
    public boolean onHover(View v, MotionEvent event) {
        Log.d("ListAdapter", "Hover: " + item);
        return false;
    }
});

It will work.

NG_
  • 6,895
  • 7
  • 45
  • 67
Nifhel
  • 2,013
  • 2
  • 26
  • 39
  • Where should I put the intent filter in the manifest? If I put this line in the intent-filter of the main activity, then Eclipse installs the app on my phone but it says that there is no launcher activity. If I don't include this line, it also works. Really great ! – Stephane Mathis May 22 '13 at 20:24
  • Hmm, ok. If you don't put that line on th emanifest, you won't get any event (it's odd that for the main activity it still works). – Stephane Mathis May 22 '13 at 21:45