View Discussion Improve Article Save Article View Discussion Improve Article Save Article 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. 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 android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="HardcodedText"> <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:Steps
to implement Vibration Patterns in Android
XML
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">
<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.
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);
bComposeVibration = findViewById(R.id.makeVibrationCompositionButton);
final Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
bComposeVibration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long[] vibrationWaveFormDurationPattern = {0, 10, 200, 500, 700, 1000, 300, 200, 50, 10};
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
VibrationEffect vibrationEffect = VibrationEffect.createWaveform(vibrationWaveFormDurationPattern, -1);
vibrator.cancel();
vibrator.vibrate(vibrationEffect);
}
}
});
}
}
Kotlin
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)
bComposeVibration = findViewById(R.id.makeVibrationCompositionButton)
val vibrator = getSystemService<Any>(Context.VIBRATOR_SERVICE) as Vibrator
bComposeVibration.setOnClickListener(object : OnClickListener() {
fun onClick(v: View?) {
val vibrationWaveFormDurationPattern =
longArrayOf(0, 10, 200, 500, 700, 1000, 300, 200, 50, 10)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val vibrationEffect =
VibrationEffect.createWaveform(vibrationWaveFormDurationPattern, -1)
vibrator.cancel()
vibrator.vibrate(vibrationEffect)
}
}
})
}
}
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.