cancel
Showing results for 
Search instead for 
Did you mean: 

How to send haptic feedback on UI hover/tap in Jetpack compose?

Hi, I am looking for a way to send small vibration as a haptic feedback on my 2D panel app whenever user hovers their raycast over a UI element, and stronger vibration when they tap on that element.
In unity, we can do something like:

XRBaseControllerInteractor.SendHapticImpulse(0.2f, 0.1f);

how to achieve similar effect in Jetpack?

           val hapticFeedback = LocalHapticFeedback.current
           Button(onClick = {

                hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)
                //nothing happens
            }) {
                Text("pick a file")
            }
1 ACCEPTED SOLUTION

Accepted Solutions

dav-s
Meta Employee

Hi! You can do this with the SpatialInterface.applyHapticFeedback() function.

// inside your AppSystemActivity somewhere
// "spatial" is a SpatialInterface inherited from VrActivity
// you can pass it around to somewhere else you need it reference it
spatial.applyHapticFeedback(
   // hand: Apply haptics to the specified controller hand.
   Hand.RIGHT,
   // amplitude: Value between 0 - 1 for strength of haptics.
   0.3f,
   // duration: Time in nanoseconds for how long the haptic feedback should be applied.
   (0.03 * 1e9).toLong(),
   // frequency`: Frequency of vibration (in hz).
   128f
)

Hopefully this should help!

View solution in original post

2 REPLIES 2

dav-s
Meta Employee

Hi! You can do this with the SpatialInterface.applyHapticFeedback() function.

// inside your AppSystemActivity somewhere
// "spatial" is a SpatialInterface inherited from VrActivity
// you can pass it around to somewhere else you need it reference it
spatial.applyHapticFeedback(
   // hand: Apply haptics to the specified controller hand.
   Hand.RIGHT,
   // amplitude: Value between 0 - 1 for strength of haptics.
   0.3f,
   // duration: Time in nanoseconds for how long the haptic feedback should be applied.
   (0.03 * 1e9).toLong(),
   // frequency`: Frequency of vibration (in hz).
   128f
)

Hopefully this should help!

chatpoint.2024
Honored Guest

In Jetpack Compose, you can use HapticFeedback for such effects, but it is limited to predefined feedback types like HapticFeedbackType.LongPress or HapticFeedbackType.TextHandleMove. Custom haptic strength like in Unity's SendHapticImpulse isn't natively supported. Here's how you can implement it:

val hapticFeedback = LocalHapticFeedback.current
Button(onClick = {
    hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)
}) {
    Text("Pick a file")
}

For finer control (e.g., small/strong vibrations), use Android's Vibrator API with performHapticFeedback:

val context = LocalContext.current
val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    vibrator.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE))
}

For more on app features like Adam WhatsApp, visit Chatpointt.com.