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






<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">
  
    <!--button to compose vibration waveforms when pressed-->
    <Button
        android:id="@+id/makeVibrationCompositionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:backgroundTint="@color/colorPrimary"
        android:text="COMPOSE VIBRATION"
        android:textColor="@android:color/white" />
  
</RelativeLayout>

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




<?xml version="1.0" encoding="utf-8"?>
    package="com.adityamshidlyali.composevibrationinandroid">
  
    <!--vibrate permission which needs to be invoked as we hard 
        accessing the vibrator hardware of the device-->
    <uses-permission android:name="android.permission.VIBRATE" />
  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
  
</manifest>

Step 4: Working with the MainActivity.java 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 MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import androidx.appcompat.app.AppCompatActivity;
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;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the button with the appropriate ID
        bComposeVibration = findViewById(R.id.makeVibrationCompositionButton);
  
        // 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() {
            @Override
            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
                    vibrator.cancel();
  
                    // now initiate the vibration of the device
                    vibrator.vibrate(vibrationEffect);
                }
            }
        });
    }
}

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :