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.

Code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/vibrate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Vibrate!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vibrate = findViewById(R.id.vibrate);
        vibrate.setOnClickListener(new View.OnClickListener() {
            @Override
            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!

Conclusion

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: