Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Compose Vibration Patterns in Android?

  • Last Updated : 23 Feb, 2021

As it is discussed in How to Vibrate a Device Programmatically in Android previously in the article. So in this article, it’s been discussed how to make vibration patterns, which produces the vibrations of different waveforms. This implementation can be a replica of the vibration produced when there is an incoming call and the device makes various patterns of vibrations. Note that we are also going to implement this project using the Java language. 

Steps to implement Vibration Patterns in Android

Step 1: Create an Empty Activity android studio project

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Step 2: Working with the activity_main.xml file

  • Implement a single button in the layout which is used to create vibration waveforms, when pressed.
  • Invoke the following code in the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
    <!--button to compose vibration waveforms when pressed-->
        android:text="COMPOSE VIBRATION"
        android:textColor="@android:color/white" />

Output UI:

Compose Vibration Patterns in Android

Step 3: Seeking to vibrate permissions

  • Seeking to vibrate permission in AndroidManifest.xml file, because we are accessing the hardware part of the device.
  • Invoke the following code inside the AndroidManifest.xml file


<?xml version="1.0" encoding="utf-8"?>
    <!--vibrate permission which needs to be invoked as we hard 
        accessing the vibrator hardware of the device-->
    <uses-permission android:name="android.permission.VIBRATE" />
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Step 4: Working with the file

  • In the previous article, it’s have discussed the predefined vibration for the Android device. But here custom waveforms are created using a long variable array.
  • One of the important points to be noted here is, while creating the waveforms in a types array of long type, the first element needs to be zero(0). This is so because at the first instance the vibrator of the device needs to be turned on then made to vibrate for the further waveforms.
  • In this case the waveform is long[] vibrationWaveFormDurationPattern = {0, 10, 200, 500, 700, 1000, 300, 200, 50, 10}; from the code below. And the first waveform is 0.
  • Invoke the following code in the file. Comments are added inside the code to understand the code in more detail.


import android.content.Context;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    Button bComposeVibration;
    protected void onCreate(Bundle savedInstanceState) {
        // register the button with the appropriate ID
        bComposeVibration = findViewById(;
        // create instance of the vibrator and initialise it with Vibrator system service
        final Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        // handle compose vibration button
        bComposeVibration.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Note: The first element needs to be 0
                long[] vibrationWaveFormDurationPattern = {0, 10, 200, 500, 700, 1000, 300, 200, 50, 10};
                // the vibration of the type custom waveforms needs the API version 26
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    // create VibrationEffect instance and createWaveform of vibrationWaveFormDurationPattern
                    // -1 here is the parameter which indicates that the vibration shouldn't be repeated.
                    VibrationEffect vibrationEffect = VibrationEffect.createWaveform(vibrationWaveFormDurationPattern, -1);
                    // it is safe to cancel all the vibration taking place currently
                    // now initiate the vibration of the device


My Personal Notes arrow_drop_up
Recommended Articles
Page :