<TouchableHighlight>
is used to create a wrapper over a component which can generate click events. It is different from TouchableWithoutFeedback in the sense that it can provide a visual change when clicked.
Below are the few important points regarding TouchableHighlight
–
1. It highlights the underlay color by reducing the opacity of view.
2. Just like TouchableWithoutFeedback, it should have only one child. Like this –
<TouchableHighlight ...> <View> <Component1 /> <Component2 /> </View> </TouchableHighlight>
3. There are 2 important props –
-
underlayColor
– Set it to the color value which you want to display when component is pressed. -
activeOpacity
– The value from 0 to 1. It sets the opacity of View when pressed. By default the value is 0.85. That is, the view gets 15% transparent when pressed.
<TouchableHighlight activeOpacity={0.6} underlayColor="#FF0000" onPress={() => alert('View Pressed!')}> <View> <Component1 /> <Component2 /> </View> </TouchableHighlight>
4. React Native recommends to use Pressable api instead of TouchableHighlight
because that is more future proof.
Code Example
import React, { useState } from "react"; import { StyleSheet, TouchableHighlight, Text, View } from "react-native"; const TouchableHighlightExample = () => { const [count, setCount] = useState(0); const [count1, setCount1] = useState(0); return ( <View style={styles.container}> <View style={styles.countContainer}> <Text>Default TouchableHighlight</Text> <Text style={styles.countText}>Count: {count}</Text> </View> <TouchableHighlight onPress={() => setCount(count + 1)} > <View style={styles.button}> <Text>Touch Here</Text> </View> </TouchableHighlight> <View style={[styles.countContainer, {marginTop: 40}]}> <Text>activeOpacity=0.6 {'\n'}underlayColor='#FF00000'</Text> <Text style={styles.countText}>Count: {count1}</Text> </View> <TouchableHighlight onPress={() => setCount1(count1 + 1)} activeOpacity={0.6} underlayColor={'#FF0000'} > <View style={styles.button}> <Text>Red Color on Touch</Text> </View> </TouchableHighlight> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "center", paddingHorizontal: 10 }, button: { alignItems: "center", backgroundColor: "#DDDDDD", padding: 10 }, countContainer: { alignItems: "center", padding: 10 }, countText: { color: "#FF00FF" } }); export default TouchableHighlightExample;
Output –
From the image you can see that the button is highlighted with red color when pressed. This is due to underlay color which got visible when the opacity of view is decreased by 40%.
In the code we are setting two TouchableHighlight components. 1st is with default values and second one with activeOpacity={0.6}
and underlayColor={'#FF0000'}
.
Live Demo
Conclusion
TouchableHighlight
is a wrapper component which adds an on click event for child component. When pressed, it decreases the opacity of the children and show the underlay color.
React Native Series
Alert
- Basic Alert
- Dismiss on Clicking Outside
- Input Fields in Alert Dialog – Prompt()
- Dark-Light Theme of Dialog
ActivityIndicator
- Basic Circular Loader
- Change size of Circular Loader
- Show/hide Circular Loader
- Change color of Circular Loader
Button
FlatList
- Simple List
- Single Item Selection from List
- Multiple Item Selection from List
- Adding separator between list items
- Multiple columns List
- Showing Message in Empty List
- Add Footer to the List
- Add Header to the List
- Horizontal List
- Inverted List
- Pull to Refresh in List
- Infinite Loading List
SectionList
ScrollView
- ScrollView
- Stick Single Item at Header
- Stick Multiple Items at Header
- Stick Item at Footer
- Hide Sticky Element on Scroll
Image
- Display Image from remote url
- Display local storage image
- Display Base64 Image
- Display Gif & Webp Images
- Adding Blur to Image
- Displaying loader for Image
- Resizemode for Images
- Setting Default Placeholder Image
- Background Image
Modal
RefreshControl
- RefreshControl
- Change Refresh Loader Color
- Change Refresh Loader Size
- Change Refresh Loader Background Color
- Title under Refresh Loader
- Change color of title under refresh loader
StatusBar
- Get StatusBar Size
- Change StatusBar Background Color
- Display StatusBar icons & text in While Color
- Display StatusBar icons & text in Dark Color
- Hiding StatusBar
- Translucent StatusBar
Switch
Text
- Adding Text
- Bold Text
- Italic Text
- Underline Text
- Selecting Text for copy-paste
- Changing Highlight Color of Text Selection
- Fit text in View box
- Clickable anchors in text
- Truncate Lengthy Text
TextInput
- Simple Input Field
- Auto Capitalize Text in Input Field
- Multiline Input Field
- Hide Cursor in Input Field
- Clear input Field using X
- Clear input Field when focused
- Change Cursor Color in Input Field
- Disable input field
- Icon at the left of Input Field
- Dark-Light Keyboard
- Avoid Overlapping of Keyboard
- Limiting Characters in Input Field
- Numeric Keyboard
- Email Id Keyboard
- Phone number Keyboard
- Url Keyboard
- Placeholder in input field
- Placeholder Color in Input Field
- Password Input Field
- Programmatically select text in Input Field
- Change Text Selection Color in Input Field
- Select Whole text in Input Field on Focus
- Write text from center in input field
- Changing underline color of input field
TouchableWithoutFeedback
TouchableHighlight
TouchableOpacity
Pressable
Appearance
AppState
ToastAndroid
Dimensions
Keyboard