nowinandroid icon indicating copy to clipboard operation
nowinandroid copied to clipboard

Add HapticFeedback in IconButton

Open kosenda opened this issue 1 year ago • 3 comments

What I have done and why

I thought it would be better to have haptics feedback to make it more obvious that the button was tapped, so I added haptics feedback to the commonly used IconButton

ref Android Developers - Compose UI - 1.8.0-alpha06

LocalHapticFeedback now provides a default HapticFeedback implementation when the Vibrator API indicates that haptics are supported. The following have been added to the HapticFeedbackType - Confirm, ContextClick, GestureEnd, GestureThresholdActivate, Reject, SegmentFrequentTick, SegmentTick, ToggleOn, ToggleOff, VirtualKey. Wear Compose long-clickable components such as Button, IconButton, TextButton, and Card now perform the LONG_PRESS haptic when a long-click handler has been supplied. (I5083d)

Android Developers - HapticFeedbackType

Confirm A haptic effect to signal the confirmation or successful completion of a user interaction..

Android Developers - Tap and press # long-press-show

What I have tried I thought that HapticFeedbackType had ToggleOn and ToggleOff, which seemed to be better, but on my device Pixel 6(Android 15), I didn't get any haptic feedback. So I thought Confirm was appropriate.

    haptics.performHapticFeedback(
        if (isChecked) HapticFeedbackType.ToggleOn else HapticFeedbackType.ToggleOff
    )
    onCheckedChange(isChecked)
},

kosenda avatar Jan 13 '25 04:01 kosenda

It is as though you need to add a VIBRATE permission. https://developer.android.com/reference/android/Manifest.permission#VIBRATE

Jaehwa-Noh avatar Jan 22 '25 08:01 Jaehwa-Noh

It did work when I added permission !

kosenda avatar Jan 25 '25 08:01 kosenda

Probably no special permissions are needed for HapticFeedback. LongPress worked on my device (Xperia 1 III Android 13) even without permissions. (ToggleOff and ToggleOn didn't seem to work though 😅 )

Unlike other haptic APIs, using HapticFeedbackConstants with a View doesn't require the VIBRATE permission.

https://developer.android.com/develop/ui/views/haptics/haptic-feedback#use_view_prereq

blue928sky avatar Apr 24 '25 04:04 blue928sky