This app shows two buttons stacked vertically. The first uses TouchableOpacity and fades when tapped. The second uses TouchableHighlight and shows a gray highlight when tapped. Both show alerts on press.
import React from 'react';
import { View, Text, TouchableOpacity, TouchableHighlight, Alert, StyleSheet } from 'react-native';
export default function App() {
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.button}
activeOpacity={0.6}
onPress={() => Alert.alert('TouchableOpacity pressed!')}
accessibilityLabel="Opacity button"
>
<Text style={styles.text}>TouchableOpacity</Text>
</TouchableOpacity>
<TouchableHighlight
style={styles.button}
underlayColor="#DDDDDD"
onPress={() => Alert.alert('TouchableHighlight pressed!')}
accessibilityLabel="Highlight button"
>
<Text style={styles.text}>TouchableHighlight</Text>
</TouchableHighlight>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
button: {
backgroundColor: '#2196F3',
paddingVertical: 12,
paddingHorizontal: 25,
marginVertical: 10,
borderRadius: 5
},
text: {
color: 'white',
fontSize: 16
}
});