Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - two items: one on the left, one in the center

I have been struggling to align two items in the following positions: the first one should be on the left side of the row and the second element needs to be in the center of the row.

The following code below does not fully center my second element:

<View style={{flexDirection: 'row', justifyContent: 'space-between'}}>

            <View style={{ paddingLeft: 10 }}>
                <Text style={{ fontSize: 20, color: 'red', fontWeight: '200' }}>LEFT_ELEM</Text>
            </View>
            <View style={{paddingRight: 10 }}>
            <Text>
                CENTER
            </Text>
            </View>
            {/* Empty view so that space-between works? */}
            <View
                style={{paddingRight: 10 }}>
            </View>
 </View>

This was the closest I could get to the result I wanted. However, it does not do the trick. Could anyone know the best way to implement this successfully?

like image 528
GIV Avatar asked Mar 23 '18 04:03

GIV


People also ask

How do you align items in Center in React Native?

How do you center text in a view React Native? To make text align center horizontally, apply this Property (textAlign:”center”) . Now to make the text align vertically, first check direction of flex. To Make text align center apply same property ( textAlign:”center” ).

How do you align text left and right on same line in React Native?

How to align icon and text in same line in react native? flexDirection: 'row' use for show content in a row. justifyContent: 'center' use for aligning vertically. alignItems: 'center' use for aligning horizontally.


2 Answers

You need to add flex: 1 to parent View and children Views (all children will have flex: 1 if you want them all to be of equal size, otherwise define width/flex for each child View individually).

Try this:

      <View style={{ flex: 1, flexDirection: 'row', justifyContent: 'space-between' }}>
        <View style={{ flex: 1, paddingLeft: 10 }}>
          <Text style={{ fontSize: 20, color: 'red', fontWeight: '200' }}>LEFT_ELEM</Text>
        </View>
        <View style={{ flex: 1, paddingRight: 10 }}>
          <Text style={{ textAlign:'center' }}>CENTER</Text>
        </View>
        <View
          style={{ flex: 1, paddingRight: 10 }}>
        </View>
      </View>

Added style={{ textAlign:'center' }} to Text in center View child to give you an idea of its centered position. You can modify/remove it.

like image 110
xuhaib Avatar answered Sep 20 '22 19:09

xuhaib


When I learned Android, I was told not to use too many 'layers' of components. In that philosophy, I decided to use 'absolute' property to the left element to achieve a simpler result. In this scheme, the 'left' item is almost stick to the left wall.

<View
    style={{
        height: 50,
        flexDirection: 'row', // a must
        alignItems: 'center', // to make items center vertically.
        justifyContent: 'center' // to make the second item center horizontally.
    }}
>
    <MaterialIcons
        style={[styles.titleIcon, { position: 'absolute', left: 0 }]} // on left, still center vertically.
        name='arrow-back'
        onPress={() => {
            navigation.goBack();
        }}
    />
    <Text style={styles.titleText}>{result.name}</Text> // on center automatically
</View>
like image 36
Brian Hong Avatar answered Sep 20 '22 19:09

Brian Hong