Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two buttons with equal width horizontally fill the screen in React Native

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.

Sample Image Here

like image 619
Vikas Jalan Avatar asked Mar 17 '17 18:03

Vikas Jalan


People also ask

How do you make a button full width in React Native?

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.

How do you center a button horizontally in React Native?

If you are using native base, you need to add flexDirection: "row", then it will solve the issue.

How do you give a space between two views in React Native?

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.


2 Answers

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

like image 187
guitoof Avatar answered Sep 18 '22 04:09

guitoof


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'   } }); 

enter image description here

 <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'   } }); 

enter image description here

like image 36
Keshav Gera Avatar answered Sep 19 '22 04:09

Keshav Gera