Android Vibrate on Click

If you want to make the phone to vibrate when you click on a button from your application:
1. You have to declare in your AndroidManifest.xml the permission:

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

2. Now go to the class you want to put the vibration and in onCreate put this code:

final Vibrator vibe = (Vibrator) yourActivity.this.getSystemService(Context.VIBRATOR_SERVICE);
//replace yourActivity.this with your own activity or if you declared a context you can write context.getSystemService(Context.VIBRATOR_SERVICE);    

3. Now make a button with a click listener. Here you will call the vibe from above.

Button vibrateButton = new Button(this);
vibrateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {               
                 vibe.vibrate(80);//80 represents the milliseconds (the duration of the vibration)

            }
        });
keyboard_arrow_up