user3286430
user3286430

Reputation:

Two buttons sharing a row in react-native

I have two buttons that look like this

enter image description here

This is the code

render = () => (
    <Image
      source={require('../../images/login.jpg')}
      style={[AppStyles.containerCentered, AppStyles.container, styles.background]}
    >
      <Image
        source={require('../../images/logo.png')}
        style={[styles.logo]}
      />
      <Spacer size={200} />
      <View style={[AppStyles.row, AppStyles.paddingHorizontal]}>
        <View style={[AppStyles.flex1]}>
          <Button
            title={'Login'}
            icon={{ name: 'lock' }}
            onPress={Actions.login}
          />
        </View>
      </View>

      <Spacer size={10} />


      <View style={[AppStyles.row, AppStyles.paddingHorizontal]}>
        <View style={[AppStyles.flex1]}>
          <Button
            title={'Sign up'}
            backgroundColor={'#FB6567'}
            icon={{ name: 'face' }}
            onPress={Actions.signUp}
          />
        </View>
      </View>

    </Image>
  )

I want the buttons to occupy one row and possibly share 40% - 40% of the space with the rest of the 20% going to the padding. How can i have them occupy one row?.

Upvotes: 10

Views: 43520

Answers (1)

sooper
sooper

Reputation: 6039

You'd need to define a container with flexDirection set to row and use justifyContent depending on where you want your padding:

render() {
  return (
    <View style={styles.container}>
      <View style={styles.button} />
      <View style={styles.button} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-between'
  },
  button: {
    backgroundColor: 'green',
    width: '40%',
    height: 40
  }
});

Change space-between to space-around if you want the padding to be distributed to the sides too. (Demo)

Upvotes: 37

Related Questions