Forum Discussion
to_the_cosmic_end
1 year agoProtege
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")
}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!
2 Replies
Replies have been turned off for this discussion
- dav-sMeta 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.2024Honored 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.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 10 months ago
- 4 years ago
- 4 years ago
- 11 days ago