Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Flexbox - React Native

i am new to react native and i am trying to make a chat app layout. enter image description here

As you can see in screenshot that i have attached, i want text box to take as much space as the text in it. For example, if i send 'Hi', it should only be stretched as much space 'Hi' needs. If i send a long text, then it should stretch and take as much space as that text is. Like it happens in almost all texting apps.

The text box code is:

<TouchableOpacity>
        <View style={styles.containerSent}>
          <View style={{flex: 3}}>
            <Text style={styles.message}>{item.message}</Text>
          </View>
          <View style={{flex: 1}}>
            <Image
              source={require('../assets/images/check.png')}
              style={styles.image}
            />
            <Text style={{textAlign: 'right', color: 'white'}}>
              {msToTime(item.timestamp)}
            </Text>
          </View>
        </View>
      </TouchableOpacity>

Stylesheet

  containerSent: {
    flex: 1,
    maxWidth: '70%',
    // minWidth: '30%',
    marginTop: 20,
    paddingTop: 20,
    paddingHorizontal: 10,
    paddingBottom: 10,
    borderWidth: 1,
    backgroundColor: '#6F2232',
    borderRadius: 20,
    alignSelf: 'flex-end',
    flexDirection: 'row',
    justifyContent: 'space-between',
    // justifyContent: 'flex-end',
  },

  message: {
    color: 'white',
    fontSize: 15,
    fontWeight: 'bold',
    textAlign: 'left',
    alignSelf: 'stretch',
  },

  image: {
    width: 15,
    height: 15,
    marginLeft: 20,
    marginBottom: 5,
    alignSelf: 'flex-end',
  },
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Set View style to flex: 0 so it‘s just taken as Maus h space as necessary.

  <TouchableOpacity>
    <View style={styles.containerSent}>
      <View style={{ flex: 0 }}>
        <Text style={styles.message}>{'q.a'}</Text>
      </View>
      <View style={{ flex: 0, paddingLeft: 10 }}>
        <Text style={styles.message}>{'eeeee.message'}</Text>
      </View>
    </View>
  </TouchableOpacity>

Take a look at my sample

about 4 years ago · Juan Pablo Isaza Report

0

How I have implemented this is that I removed flex: 3 and flex: 1 from both the elements so that they don't expand to take the max space, and I added flexShrink: 1 to the text, so that it always shrinks keeping the right element intact.

Snack link

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!