React Native Touchable – 4 Different Type of Touchables

4 Different Type of React Native Touchables

In this post, you will see 4 Different Type of React Native ‘Touchable’. React Native Touchable is a component to overcome the limitation of the styling of the button component. There are four types of Touchables. Which are

S. No.TouchableAvailable For
1.TouchableNativeFeedbackOnly For Android
2.TouchableHighlightFor Android/IOS Both
3.TouchableOpacityFor Android/IOS Both
4.TouchableWithoutFeedbackFor Android/IOS Both

1. TouchableNativeFeedback

A wrapper for making views respond properly to touches (Android only). On Android, this component uses native state drawable to display touch feedback.

To Import TouchableNativeFeedback in Code

import { TouchableNativeFeedback} from 'react-native'

Render Using

<TouchableNativeFeedback>
    <View>
        <Text>
            Label
        </Text>
    </View>
</TouchableNativeFeedback>

2. TouchableHighlight

A wrapper for making views respond properly to touches. On key down, the opacity of the wrapped view is decreased, which allows the underlying color to show through, darkening or tinting the view.

To Import TouchableHighlight in Code

import { TouchableHighlight} from 'react-native'

Render Using

<TouchableHighlight>
    <Text>
        Label
    </Text>
</TouchableHighlight>

3. TouchableOpacity

A wrapper for making views respond properly to touches. On key down, the opacity of the wrapped view is decreased, dimming it.

To Import TouchableOpacity in Code

import { TouchableOpacity} from 'react-native'

Render Using

<TouchableOpacity>
    <Text>
        Label
    </Text>
</TouchableOpacity>

4. TouchableWithoutFeedback

Don’t use unless you have a very good reason. All elements that respond to press should have visual feedback when touched but it doesn’t have any.

To Import TouchableWithoutFeedback in Code

import { TouchableWithoutFeedback} from 'react-native'

Render Using

<TouchableWithoutFeedback>
    <View>
        <Text>
            Label
        </Text>
    </View>
</TouchableWithoutFeedback>

In this example, we will see all the four Touchables. So Lets’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Code

Open App.js in any code editor and replace the code with the following code

App.js

//This is an example code to understand Touchable// 
import React, { Component } from 'react';
//import react in our code. 
import { TouchableNativeFeedback, TouchableHighlight,TouchableOpacity,
         TouchableWithoutFeedback, View, Text, StyleSheet} from 'react-native';
//import all the components we are going to use. 
export default class App extends Component {
    onPress(){
      //Do what you want
    }
   render() {
      return (
         <View style = {styles.container}>
         {/*Only for android If you trieng to run in IOS please remove 
            TouchableNativeFeedback section*/}
          <TouchableNativeFeedback onPress={this.onPress}
              background={TouchableNativeFeedback.SelectableBackground()}>
            <View style={styles.button}>
              <Text>Touchable Native Feedback(Only Android)</Text>
            </View>
          </TouchableNativeFeedback>
          <TouchableHighlight style={styles.button} onPress={this.onPress}>
            <Text>Touchable Highlight</Text>
          </TouchableHighlight>
          <TouchableOpacity
              style={styles.button}
              onPress={this.onPress}>
            <Text>Touchable Opacity</Text>
          </TouchableOpacity>
          <TouchableWithoutFeedback
              style={styles.button}
              onPress={this.onPress}>
            <View style={styles.button}>
              <Text>Touchable Without Feedback</Text>
            </View>
          </TouchableWithoutFeedback>
         </View>
      )
   }
}
const styles = StyleSheet.create ({
   container: {
      flex: 1,
      alignItems: 'center',
      marginTop: 50
   },
    button: {
      alignItems: 'center',
      backgroundColor: '#DDDDDD',
      padding: 10,
      width:300,
      marginTop:16
    },
});

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshot

Output in Online Emulator

That was the 4 Different Type of React Native Touchable provided by React Native Core library.

React Native Platform Touchable using react-native-platform-touchable

If you want to explore more then you can see React Native Platform Touchable which is a built-in wrapper over the various React Native Touchable.

First of all why we need that?

If we talk about TouchableNativeFeedback, it provides a Native experience but it is only supported by the Android not iOS that is why we use TouchableOpacity as an alternative solution.

In the above situation, we have two choices

  1. Either we use a common Touchable which is TouchableOpacity (Best for both platform)
  2. Identify the platform and then apply the Touchable accordingly

If you want to use the first method then it’s OK, you can proceed with that but if you want to provide the Native experience to your App users then you should go for the second one which makes the code lengthy (not much but a little bit).

react-native-platform-touchable library bought a simple component Touchable for the second case mentioned above. It is a wrapper over various Touchables which makes life very easy. You just have to install the dependency and you are good to go. It will handle all the conditions and will use the Touchable according to the platform to provide the best experience.

For iOS:

TouchableOpacity with default activeOpacity.

For Android:

TouchableNativeFeedback with background from Android app style for Android API <= 20

TouchableOpacity for Android < 5.0

In this example, we will make a simple button with a Touchable component to show the rendering of different Touchables according to a different platform. So let’s get started.

Import React Native Platform Touchable

import Touchable from 'react-native-platform-touchable';

To Make Touchable

<Touchable
  onPress={() => console.log('Button Clicked!')}
  style={{
    backgroundColor: '#FF4500',
  }}
  background={Touchable.Ripple('#FFD700')}>
  <Text
    style={{
      color: 'white',
      fontSize: 18,
      textAlign: 'center',
      padding: 15,
    }}>
    {Platform.OS === 'android'
      ? 'Device: Android \n Touchable Type: TouchableNativeFeedback'
      : 'Device: iOS \n Touchable Type: TouchableOpacity'}
  </Text>
</Touchable>

For this example, we will make a home screen with 1 button using Touchable.

Installation of Dependency for React Native Platform Touchable

To use Touchable we need to install react-native-platform-touchable dependency.

To install this open the terminal and jump into your project using

cd ProjectName

Run the following command to install

npm install react-native-platform-touchable --save

This command will copy all the dependency into your node_module directory.

–save is optional, it is just to update the react-native-platform-touchable dependency in your package.json file.

Code for React Native Platform Touchable

Now Open App.js in any code editor and replace the code with the following code

App.js

/*This is an example of React Native Touchable using react-native-platform-touchable*/
import React from 'react';
//import React

import { Text, View, Platform } from 'react-native';
//import Basic Component

import Touchable from 'react-native-platform-touchable';
//import Touchable

export default class App extends React.Component {
  render() {
    return (
      <View
        style={{
          flex: 1,
          alignItems: 'center',
          justifyContent: 'center',
          padding: 20,
        }}>
        <Touchable
          onPress={() => console.log('Button Clicked!')}
          style={{
            backgroundColor: '#FF4500',
          }}
          background={Touchable.Ripple('#FFD700')}>
          <Text
            style={{
              color: 'white',
              fontSize: 18,
              textAlign: 'center',
              padding: 15,
            }}>
            {Platform.OS === 'android'
              ? 'Device: Android \n Touchable Type: TouchableNativeFeedback'
              : 'Device: iOS \n Touchable Type: TouchableOpacity'}
          </Text>
        </Touchable>
      </View>
    );
  }
}

Output Screenshots for React Native Platform Touchable

Android

IOS

Output in Online Emulator for React Native Platform Touchable


This is how you can Make React Native Touchable using react-native-platform-touchable. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂


Related Posts

4 thoughts on “React Native Touchable – 4 Different Type of Touchables”

    • Button has no style prop you can only change the color using the color prop. If you want to do some customization you need to use React Native Touchable. Touchables have customization options.

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.