Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable haptic Feedback on react-native touchable view

I an writing a react-native app, And I noticed that while the buttons look like native buttons when clicked - they do not behave as one (At lease not like android button behave).

Clicking on android app button - make a sound and give the user an haptic Feedback. On the sound I saw that there is github discussions and open issue, but I could not found anywhere anything about the haptic Feedback.

How can I make my view (any touchable view..) to make haptic feedback on click? This is really important feeling in an app.

I want something like this (In android)

View view = findViewById(...)
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

This doesn't require from the app to have permission for Vibrate.

And managing haptic feedback on my own with the Vibrate api will cause users that disable this option globally to experience bad behavior

Thanks

like image 926
Nadav Nagel Avatar asked May 03 '17 12:05

Nadav Nagel


People also ask

What is touchablewithoutfeedback in React Native?

Importantly, TouchableWithoutFeedback works by cloning its child and applying responder props to it. It is therefore required that any intermediary components pass through those props to the underlying React Native component. When true, indicates that the view is an accessibility element. By default, all the touchable elements are accessible.

Is it possible to implement haptic feedback on Android?

Right now the Android implementation is a small Vibrate pattern, similar to the "feeling" of the iOS haptic system. Android needs to be View bound to trigger the real haptic engine. So i want to enhance the Library to support a <TouchableWithHapticFeedback>.

How to make views respond properly to touches?

A wrapper for making views respond properly to touches (Android only). On Android this component uses native state drawable to display touch feedback. At the moment it only supports having a single View instance as a child node, as it's implemented by replacing that View with another instance of RCTView node with some additional properties set.

How to customize background drawable of native feedback touchable?

Background drawable of native feedback touchable can be customized with background property. Inherits TouchableWithoutFeedback Props. Determines the type of background drawable that's going to be used to display feedback. It takes an object with type property and extra data depending on the type.


Video Answer


3 Answers

If you are using Expo with react-native, I recommend you to use Expo haptics

After installing the library You can use it like:

<TouchableOpacity onPress = { ()=> { 
     Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success)
     or
     Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light) 
     or
     Haptics.selectionAsync()
   
 } } > </TouchableOpacity>
like image 125
A.r. Basith Avatar answered Oct 19 '22 11:10

A.r. Basith


I found this component on github https://github.com/charlesvinette/react-native-haptic

I didn't try it yet, but it should help you to get the haptic feedback you want.

like image 2
David Avatar answered Oct 19 '22 12:10

David


I also have nearly the same requirements and I ended up using this library react-native-haptic-feedback.

Keep in mind that haptic feedback is available only on some latest android devices and in iOS above iPhone 6s. Here is a simple code snippet:

import ReactNativeHapticFeedback from "react-native-haptic-feedback";

const options = {
  enableVibrateFallback: true,
  ignoreAndroidSystemSettings: false
};

ReactNativeHapticFeedback.trigger("impactMedium", options);

In your case, it will work directly with the button's onPress method such as:

<TouchableOpacity onPress={()=>{ReactNativeHapticFeedback.trigger("impactMedium", options);}} />

Note: I found that the most versatile type which is supported by maximum devices is impactMedium.

like image 2
Umang Loriya Avatar answered Oct 19 '22 11:10

Umang Loriya