Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to centered TextInput on page in react-native?

When TextInput focused and android keyboard opened need to centered TextInput on screen above keyboard. Ho to make it?

like image 597
zlFast Avatar asked Mar 27 '26 04:03

zlFast


2 Answers

For some reason justifyContent is not working for TextInput so just use margins. Here is example:

var styles = StyleSheet.create({
  textInput: {
    marginLeft: 50,
    marginRight: 50
  }
});

Just make sure to set styles to TextInput:

<TextInput style={styles.textInput} />

Almost forgot - do not set width, use margins to define size, or it won't work.

like image 66
Aleksandar Popovic Avatar answered Mar 28 '26 18:03

Aleksandar Popovic


You'll need to use flexbox.

On the parent container you'll need something like:

var styles = StyleSheet.create({
    contaner: {
      display: 'flex',
      flexDirection: 'row',
      justifyContent: 'center
    }
});

Here is a good guide to flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

like image 25
RickTakes Avatar answered Mar 28 '26 19:03

RickTakes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!