Open In App

How to Compose Vibration Patterns in Android?

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



Step 2: Working with the activity_main.xml file




<?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:



Step 3: Seeking to vibrate permissions




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    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




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);
                }
            }
        });
    }
}




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;
 
class MainActivity : AppCompatActivity() {
    var bComposeVibration: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        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
        val vibrator = getSystemService<Any>(Context.VIBRATOR_SERVICE) as Vibrator
 
        // handle compose vibration button
        bComposeVibration.setOnClickListener(object : OnClickListener() {
            fun onClick(v: View?) {
 
                // Note: The first element needs to be 0
                val vibrationWaveFormDurationPattern =
                    longArrayOf(0, 10, 200, 500, 700, 1000, 300, 200, 50, 10)
 
                // the vibration of the type custom waveforms needs the API version 26
                if (Build.VERSION.SDK_INT >= 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.
                    val 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)
                }
            }
        })
    }
}
//This code is added by Ujjwal Kumar Bhardwaj

Output:


Article Tags :