Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: How to set <TextInput/>'s height and width to <View/> container?

Tags:

I currently have a <TextInput> inside a <View> that has a padding: 15. I would like for the <TextInput>'s width and height to cover all space inside <View> except the padding.

So I tried var width = Dimensions.get('window').width and the following, but the < TextInput > abide to the padding on the left but when you continue to type, it goes beyond the right side padding:

<View style = {{ padding: 15 }}> <TextInput style = {{ width: width }} /> </View>

So how can I get the TextInput to cover all space, height and width, inside View yet abide to the View's padding rule as well?

Thank you

like image 694
Walter Avatar asked Sep 28 '16 05:09

Walter


People also ask

How do I change the height of TextInput in React Native?

To grow height of TextInput upon text wrapping with React Native, we can set the height of the TextInput to the content's height when the content size changes. to set the onContentSizeChange prop to a function that calls setHeight with event. nativeEvent. contentSize.

How do you set dynamic height and width in React Native?

In react-native, to set the dynamic width and height to a component, we use built-in interface Dimensions. it takes the dimension of a given component and then sets the width and height to its equivalent.

How do I set the width and height of an image in React Native?

So all we need to do is create a View with width: "100%" , then use onLayout to get the width of that view (i.e. the container width), then use that container width to calculate the height of our image appropriately.

How do you use height and width in React Native?

import { Dimensions } from 'react-native'; You can get the application window's width and height using the following code: const windowWidth = Dimensions. get('window').


2 Answers

Try setting the styling of TextInput to flex: 1 instead of getting the width. The Flex style will automatically fill your view and leave the padding blank.

<View style = {{ padding: 15 }}> <TextInput style = {{ flex: 1 }} /> </View>
like image 80
rudydydy Avatar answered Sep 22 '22 18:09

rudydydy


Another good answer would be to add:

alignItems: 'stretch'

alignItems: 'stretch' will stretch every child element along the Cross Axis as long as the child element does not have a specified width (flexDirection: row) or height (flexDirection: column). ​

In your container if you have one, something like:

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'stretch',
  }
});
like image 44
Gonzalo Ortellado Avatar answered Sep 24 '22 18:09

Gonzalo Ortellado