Android Tutorial on Vibration | How to vibrate device on button click


Introduction :

Android has got a lot of features which all of us know and in every tutorial i am using one of it to make our app and this time i have brought a new tutorial topic vibration which you might have experienced a lot of times.

People use this feature to avoid loud sound or music when we receive a phone call or a message or any other device broadcast we may use silent but we will not get notified.

Vibration mode is useful in these kinds of situations by alerting user regarding the incoming call or any other services like notifications, messages…

Yes, you are right vibration is not only a feature of android but you can also find it in many basic devices.Here, we are going to see how to implement it in android tutorial on vibration.


Android Tutorial on Vibration :

Generally vibration mode is used to notify user regarding any message, phone call, when we deactivate sounds i.e., ringtones and also for any other notifications which may include alert / error messages.

So here i think now you got a basic knowledge regarding vibration mode, or uses of vibration, so we can move forward.


Note :  Android vibration tutorial is updated because of the latest android changes in the new versions starting from Api 26 and i have provided the code below after the previous code do go through it and let me know in comment section if any help required.


Creating activity_vibrate.xml :


Its a simple interface for android tutorial on vibration where just we require a button.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

        android:background="@drawable/vibrate" />



Creating a :

Here we will be using vibrator service which is a part of system services in android.

Vibrator = (Vibrator)getSystemService(MainActivity.VIBRATOR_SERVICE);


package vibrator.androidcoding.abhishek.vibrator;

import android.os.Bundle;
import android.os.Vibrator;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;

import vibrator.androidcoding.abhishek.vibrator.R;

public class MainActivity extends Activity {

    Button buttonVibrate;

    Vibrator Vibrator;

    public void onCreate(Bundle savedInstanceState) {
        buttonVibrate = (Button)findViewById(;

        /*Vibrator = (Vibrator)getSystemService(MainActivity.VIBRATOR_SERVICE);*/

        buttonVibrate.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub




// new code update which latest devices supports old and new devices
private void vibrate() {

if (Build.VERSION.SDK_INT >= 26) {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(1000,10));
    } else {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(1000);


AndroidManifest.xml :

We need to add a permission for vibration and this is the crucial step as without permission your app may not be able to make device vibrate. 

Also make sure you accept the user permission before making use of this service.

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



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="vibrator.androidcoding.abhishek.vibrator" >

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

        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



Android Tutorial on Vibration Output :

This screen depicts the Android Tutorial on Vibration


Android Tutorial on Vibration



If any query in this tutorial on Android Tutorial on Vibration do let us know in the comment section below.

Like and share the tutorial for more interesting android tutorials.

Show Buttons
Hide Buttons
Read previous post:
Android spinner drop down arrow color change

  Spinner drop down button color : When using android spinner we sometimes want to change the color of the...