Add HapticFeedback in IconButton
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
ConfirmA 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)
},
It is as though you need to add a VIBRATE permission. https://developer.android.com/reference/android/Manifest.permission#VIBRATE
It did work when I added permission !
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