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.

  • Unable to resolve target 'android-21'
  • How to change text color of simple list item
  • Gmail API access using Android
  • Getting the size of the window WITHOUT title/notification bars
  • Trouble getting Android Support V7 CardView Library to work
  • Continuous Integration for Android via TeamCity. Preferred tools?
  • Related posts:

    Filter LogCat to get only the messages from My Application in Android?
    Android emulator system partition no space from start
    How to identify three finger tap in android
    Can't find getPackageManager() method in android
    Facebook Android SDK Invalid_key
    How to use setOnTouchListener in C# (Xamarin)
  • What is the role of content_main.xml in android studio 1.4?
  • IllegalStateException: Activity has been destroyed - when app is trying to show DialogFragment once more time
  • Media Recorder with Google Vision API
  • e.printStackTrace(); in string
  • How to create layout-small-land folder?
  • Android: How to use selector for background of a TextView?
  • 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.