<TouchableOpacity>
is a wrapper component which enables the press event for child component. Unlike TouchableHighlight, it only decreases the opacity of view as a visual feedback, when pressed.
Some of the properties of TouchableOpacity
are –
1. It decreases the opacity of view to show that it is pressed.
2. React Native recommends using Pressable api instead of this because that is more future proof.
3. It can only have single child. If you need to add more child then use the enclosing View. Like this –
<TouchableOpacity onPress={onPress}> <View> <Component1 /> <Component2 /> </View> </TouchableOpacity>
4. Use activeOpacity
prop to set the opacity of child component when pressed. If activeOpacity=0.6
then view will be 40% transparent. By default it is 0.2.
Code Example
import React, { useState } from "react"; import { StyleSheet, TouchableOpacity, Text, View } from "react-native"; const TouchableOpacityExample = () => { const [count, setCount] = useState(0); const [count1, setCount1] = useState(0); return ( <View style={styles.container}> <View style={styles.countContainer}> <Text>Default TouchableOpacity - 0.2</Text> <Text style={styles.countText}>Count: {count}</Text> </View> <TouchableOpacity onPress={() => setCount(count + 1)} > <View style={styles.button}> <Text style={{color: '#FFFFFF'}}>Touch Here</Text> </View> </TouchableOpacity> <View style={[styles.countContainer, {marginTop: 40}]}> <Text>activeOpacity=0.6</Text> <Text style={styles.countText}>Count: {count1}</Text> </View> <TouchableOpacity onPress={() => setCount1(count1 + 1)} activeOpacity={0.6} > <View style={styles.button}> <Text style={{color: '#FFFFFF'}}>Opacity - 0.6</Text> </View> </TouchableOpacity> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "center", paddingHorizontal: 10 }, button: { alignItems: "center", backgroundColor: "#FF0000", padding: 10, }, countContainer: { alignItems: "center", padding: 10 }, countText: { color: "#FF00FF" } }); export default TouchableOpacityExample;
Output –
In the above image you can see that we have both iOS and Android phones. On iOS we are clicking the first button and on Android we are clicking the second button. By default the TouchableOpacity
is 0.2 so the first button is more faded than the second button in Android phone. Because on 2nd button, we are using opacity=0.6.
In our code we are setting activeOpacity={0.6}
in line 28.
Live Demo
Conclusion
TouchableOpacity
is just like TouchableHighlight
with the difference that it doesn’t show underlay color. It only changes the opacity of view to give the impression of click.
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