How to enable haptic feedback on button view

AndroidVibration

Android Problem Overview


I want to add haptic feedback to my application's buttons and control them programmatically to show button state (enabled and disabled). The default haptic feedback setter works only for long press. How can i make it work for simple button clicks.

And is there a way to have haptic feedback on events like touch move?

Android Solutions


Solution 1 - Android

UPDATE ON DECEMBER 24TH 2019:

The view must be enabled Haptic function by:

  1. Add android:hapticFeedbackEnabled="true" in xml.
  2. Or use view.setHapticFeedbackEnabled(true); in code

(Cited from Ivan Chau)

However, one more thing to take into consideration is to enable Haptic Setting in virtual devices. This is annoying sometimes, so we have some flags come to help (which will ignore these enable Setting somehow):

view.performHapticFeedback(
    HapticFeedbackConstants.VIRTUAL_KEY, 
    HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING  // Ignore device's setting. Otherwise, you can use FLAG_IGNORE_VIEW_SETTING to ignore view's setting.
);

An example to Mayra is, for run the Haptic Feedback is by using this code.

View view = findViewById(...)
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

And this line of code can easy be include in you onclick action. The good part with this is you do not need to set a permission in the AndroidManifest (I do not need this on SdkVersion "7" (2.1 or 2.3 is 7 ))

Also note, in my code here, this will only be running if the user has enabled Haptic Feedback as global. See http://developer.android.com/reference/android/view/HapticFeedbackConstants.html for alway use it.

Solution 2 - Android

Here is an answer, though it might not be the best implementation:

import android.view.View;
import android.os.Vibrator;

public class Main extends Activity implements OnClickListener
{
    private View myView;
    private Vibrator myVib;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);

        //myView can be any type of view, button, etc.
        myView = (View) this.findViewById(R.id.myView);
        myView.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v)
    {
	    myVib.vibrate(50);
        //add whatever you want after this
    }
}

Don't forget, you also need to add the "android.permission.VIBRATE" permission to the program's manifest. You can do so by adding the following to the "AndroidManifest.xml" file:

<uses-permission android:name="android.permission.VIBRATE"/>

I hope that helps.

Solution 3 - Android

View has a performHapticFeedback function, which should allow you to perform it whenever you want, i.e., on an OnClick listener.

Solution 4 - Android

getWindow().getDecorView().performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);

a straightforward approach you can use in an activity.

Solution 5 - Android

In addition to the previous answers please make sure that "Vibration Feedback" option is enabled from your device settings

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKshitij AggarwalView Question on Stackoverflow
Solution 1 - AndroidFIG-GHD742View Answer on Stackoverflow
Solution 2 - AndroidRyanMView Answer on Stackoverflow
Solution 3 - AndroidCheryl SimonView Answer on Stackoverflow
Solution 4 - AndroidHamidreza VakilianView Answer on Stackoverflow
Solution 5 - AndroidHobyView Answer on Stackoverflow