android phone vibrate

Java/Android – How to make the device vibrate!

Working on an application that needs some sort of vibration? Say no more! In this short tutorial, you will learn how to make your application vibrate on click!

Java/Android – How to make the device vibrate!

In our last tutorial, we spoke about single selection in the Android environment. Today I will show you how to make your device vibrate programmatically in Java.

Before we start, let’s create a new project and add the permissions needed. In this tutorial, we will only need one permission.

Open AndroidManifest.xml and add the following line:

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

Now let’s go straight into our activity_main.xml and add a Button that will trigger the vibration. A normal Button will be enough, just make sure to give it an ID so we can get hold of it in the MainActivity.


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""

        app:layout_constraintTop_toTopOf="parent" />

Now we will write a simple onClick-method for the Button we created and put the vibration logic inside.

You can either build a function and call the function inside the onClick-method or just write the logic directly into the onClick-method.

Of course, we want to make sure our little function will work on any Android version, so after checking the official documentation I came up with the following;

public class MainActivity extends AppCompatActivity {
    Button vibrate;

    protected void onCreate(Bundle savedInstanceState) {
        vibrate = findViewById(;
        vibrate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT >= 26) {
                    ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
                } else {
                    ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(150);

The number in brackets stands for milliseconds. This is the value to control the length of the vibration. Put a higher value for longer vibration or a lower value for shorter vibration.

And that’s it! Run your app on any Android device and press the button. You should feel the vibration right away!


There are many situations in which vibration is needed for applications. Considering the fact that adding this little feature is very simple and takes only about 5 minutes, everyone developing Android should try it out!

If you want to read more about device vibration please visit this link.

1 thought on “Java/Android – How to make the device vibrate!”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: