I need to create two or more buttons which will be of equal width and horizontally aligned, based on screen width button width may vary.
To add a full width button with flex-box in React Native, we can set the alignSelf style to 'stretch' . to add a Button with the alignSelf style set to 'stretch' to make the button stretch to the width of the screen.
If you are using native base, you need to add flexDirection: "row", then it will solve the issue.
To add space between two elements on the same line in React Native, we can set justifyContent to 'space-between' in the View . to set the flexDirection to 'row' and the justifyContent style to 'space-between' to spread the Text components on the screen.
You can wrap you Buttons into flexed Views :
import React, { Component } from 'react'; import { Button, View, StyleSheet } from 'react-native'; export default const FlexedButtons () => ( <View style={styles.container}> <View style={styles.buttonContainer}> <Button title="Button 1"/> </View> <View style={styles.buttonContainer}> <Button title="Button 2"/> </View> </View> ); const styles = StyleSheet.create({ container: { flex: 1, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', }, buttonContainer: { flex: 1, } });
Here is a working example on Sketch: https://snack.expo.io/SyMpPSise
import React, { Component } from 'react'; import { Button, StyleSheet, View } from 'react-native'; export default class ButtonExample extends Component { _onPressButton() { alert('You tapped the button!') } render() { return ( <View style={styles.container}> <View style={styles.buttonContainer}> <Button onPress={this._onPressButton} title="Press Me" /> </View> <View style={styles.buttonContainer}> <Button onPress={this._onPressButton} title="Press Me" color="#841584" /> </View> <View style={styles.alternativeLayoutButtonContainer}> <Button onPress={this._onPressButton} title="This looks great!" /> <Button onPress={this._onPressButton} title="OK!" color="#841584" /> </View> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', }, buttonContainer: { margin: 20 }, alternativeLayoutButtonContainer: { margin: 20, flexDirection: 'row', justifyContent: 'space-between' } });
<View style={styles.menuContainer}> <TouchableOpacity onPress={() => this.pressLink('Home')}> <View style={styles.imageTextContainer}> <Image source={require('./on.png')} /> <Text style={{flex:1 ,color: '#fff',fontSize: 20,marginLeft: 20}} >Home</Text> </View> </TouchableOpacity> <TouchableOpacity onPress={() => this.pressLink('About')}> <View style={styles.imageTextContainer}> <Image source={require('./on.png')} /> <Text style={{flex:1 ,color: '#fff',fontSize: 20,marginLeft: 20}} >About</Text> </View> </TouchableOpacity> </View> const styles = StyleSheet.create({ menuContainer: { flex: 0.52, marginLeft: 5 }, imageTextContainer: { marginLeft: 20, padding: 10, flexDirection: 'row', justifyContent: 'space-between' } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With