Android Vibrations in Jetpack Compose

Code With Eaz
3 min readJan 21, 2024

--

In this blog post, we will delve into the realm of haptic feedback and explore how to seamlessly integrate vibration into your Compose applications. By the end of this guide, you’ll have a thorough understanding of implementing vibration, enhancing the user experience and adding that extra touch of interactivity to your apps.

1. Adding Permissions to AndroidManifest.xml

The AndroidManifest.xml file, nestled within the manifests package of your application, serves as the blueprint for your app’s configuration. To enable vibration features, it’s imperative to include the requisite permission. Failure to do so can hinder the functionality of vibrations within your application.

<!-- Add the following permission for vibration -->
<uses-permission android:name="android.permission.VIBRATE" />

By incorporating the <uses-permission> tag with the permission android.permission.VIBRATE, you signal to the Android system that your application is authorized to employ vibration functionalities.

2. Integrating Vibration Controls into the User Interface

With the prerequisite permissions seamlessly incorporated into the manifest, the subsequent phase involves the creation of an intuitive user interface, enabling users to effortlessly initiate vibrations. In pursuit of this goal, we design a strategically positioned Column housing buttons, each equipped with distinct vibration logic implemented within their respective onClick{} handlers.

Composing the Vibration Buttons:

// Creating a Centered Column for the Vibration Buttons
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
// Vibration Controller
val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

// Vibration Button - OneShot
Button(onClick = {
// Safely cancel any ongoing vibrations
vibrator.cancel()

// Handling vibrations for Android 8.0 (Oreo) and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val effect = VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE)
vibrator.vibrate(effect)
} else {
// Handling vibrations for devices below Android 8.0
vibrator.vibrate(100)
}
}) {
Text(text = "Vibration Button OneShot")
}

// Vibration Button - Predefined Effect (Android 10 or above)
Button(onClick = {
// Safely cancel any ongoing vibrations
vibrator.cancel()

// Checking for Android 10 or above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val effect = VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
vibrator.vibrate(effect)
} else {
// Handling vibrations for devices below Android 10
vibrator.vibrate(100)
}
}) {
Text(text = "Vibration Button Predefined")
}
}

In the provided code snippet, a Column layout is meticulously configured to occupy the entire screen space, centered both horizontally and vertically. The vibrator instance, obtained through the getSystemService call, becomes the focal point for controlling vibrations within our application.

Exploring Vibration Logic:

The onClick{} handlers for each button are structured to manage vibrations based on the Android version of the user's device. For devices running Android 8.0 (Oreo) and above, we leverage the VibrationEffect class to create nuanced vibration patterns. On the other hand, for devices below Android 8.0, a straightforward duration-based vibration is employed.

The second button introduces a more advanced feature available for devices running Android 10 or above. By utilizing the VibrationEffect.createPredefined() method, we tap into predefined vibration patterns such as EFFECT_CLICK. This enhancement offers a more diverse and expressive range of haptic feedback for users on modern Android versions; You can read more about all methods that you can use in here.

This strategic approach ensures a seamless integration of vibration controls into your user interface, enhancing the overall user experience. As we progress through this blog post, further exploration awaits on the customization and optimization of haptic interactions within Jetpack Compose. Stay tuned for a deeper dive into the world of Android development!

--

--