Android Vibrate on touch?

I am trying to make my device vibrate when I touch an object on Screen. I am using this code:

 Vibrator v = (Vibrator) getSystemService(getApplicationContext().VIBRATOR_SERVICE); 
 v.vibrate(300);    

with the permission in the manifest file but I don’t seem to get any results.
Any suggestions? Also, my hardware supports vibrate.

  • Android SDK manager SSL peer shut down incorrectly
  • Android ConstraintLayout generates absolute values
  • java.lang.NoClassDefFoundError: com.google.firebase.FirebaseOptions
  • Remove an onclick listener
  • In Android development, what files should be committed to a repository?
  • DropBoxManager use cases?
  • Android eclipse editor: Unable to edit properties
  • OutOfMemoryError at AbstractStringBuilder enlargeBuffer
  • MalformedJsonException with Retrofit API?
  • What's the difference between android:height and android:layout_height?
  • setting transparency to buttons in android
  • Invoke native date picker from web-app on iOS/Android
  • 3 Solutions collect form web for “Android Vibrate on touch?”

    please try this :

    Button b = (Button) findViewById(R.id.button1);
        b.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                Vibrator vb = (Vibrator)   getSystemService(Context.VIBRATOR_SERVICE);
                vb.vibrate(100);
                return false;
            }
        });
    

    and add this permission to manifest.xml

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

    According to this answer, you can perform haptic feedback (vibrate) without asking for any extra permissions. Look at performHapticFeedback method.

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

    Note: I have not tested this code.

    This will vibrate once, when user touched view (will not keep vibrating when user still moves his finger!):

    @Override
    public boolean onTouch(View view, MotionEvent event) {
    
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(VIBRATE_DURATION_MS);
        }
        return true;
    }
    

    And as Ramesh said, allow permission in manifest:

    <uses-permission android:name="android.permission.VIBRATE"/>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.