Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Haptic feedback: onClick() event vs hapticFeedbackEnabled in the view

Tags:

java

android

If you want a button to provide haptic feedback (ie, the phone vibrates very briefly so you can feel that you really pushed the button), what's the standard way to do that? It seems you can either explicitly set an onClick() event and call the vibrate() function, giving a number of milliseconds to vibrate, or you can set hapticFeedbackEnabled in the view.

The documentation seems to indicate that the latter only works for long-presses or virtual on-screen keys: http://developer.android.com/reference/android/view/View.html#performHapticFeedback(int)

If that's right, then I need to either make my button a virtual on-screen key or manually set the onClick() event.

What do you recommend?

Also, if I want the vibrating to happen immediately when the user's finger touches the button, as opposed to when their finger "releases" the button, what's the best way to accomplish that?

Related question: How to enable haptic feedback on button view

like image 294
dreeves Avatar asked May 13 '10 20:05

dreeves


1 Answers

There is a longer description in the Android Cookbook - Chapter Haptic Feedback

Abstract: there are several step needed:

  • enable vibration in manifest
  • enable haptic feedback for the button/view
  • register an onTouch Listener for your button
  • call performHapticFeedback() on Touch down

You can have both a onTouchLister for the haptic feedback and a onClickHandler for your action, just make sure the the onTouchHandler returns false, otherwise the event is marked as consumed and not given to the onClickHandler.

like image 78
Meier Avatar answered Sep 20 '22 19:09

Meier