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
- Create an empty activity Android studio project. And select Java as the programming language.
- Refer to Android | How to Create/Start a New Project in Android Studio to know how to create an empty activity Android Studio project.
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" ?>
< RelativeLayout
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
- 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" ?>
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 and 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.
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:
- Testing this application would be better in the physical Android devices with API version 26.
- To know how to set up the physical Android device refer to How to Run the Android App on a Real Device.