Skip to main content

React Native Positioning Element with Flex

In the previous article Layout and Flexbox, we discuss about the Flexbox and its properties. In this tutorial, we will set the position of elements with Flex.

Example 1

Create a View component and place two elements TextInput and Button. The View component with flex property (1) occupy full space of device. The elements TextInput and Button are set in default flex axis (as column).

  1. import React, { Component } from "react";  
  2. import { StyleSheet, TextInput, View , Button } from "react-native";  
  3.   
  4. export default class App extends Component {  
  5.     state = {  
  6.         placeName: "",  
  7.         places: []  
  8.     };  
  9.   
  10.     placeNameChangedHandler = val => {  
  11.         this.setState({  
  12.             placeName: val  
  13.         });  
  14.     };  
  15.   
  16.     placeSubmitHandler = () => {  
  17.         alert("button clicked")  
  18.     };  
  19.   
  20.     render() {  
  21.         return (  
  22.             <View style={styles.container}>  
  23.                 <TextInput  
  24.                         placeholder="An awesome place"  
  25.                         onChangeText={this.placeNameChangedHandler}  
  26.                         style={styles.placeInput}  
  27.                 />  
  28.                 <Button  
  29.                         title="Button"  
  30.                         onPress={this.placeSubmitHandler}  
  31.                 />  
  32.             </View>  
  33.         );  
  34.     }  
  35. }  
  36.   
  37. const styles = StyleSheet.create({  
  38.     container: {  
  39.         flex: 1,  
  40.         padding: 26,  
  41.         backgroundColor: "#fff",  
  42.         justifyContent: "flex-start"  
  43.     }  
  44. });  
Output:

React Native Positioning Element with Flex

Example 2

In this example, we will place the Button right to TextInput element. Add a child View component inside parent View with flex: 1 and flexDirtection : "row". Setting flex: 1 for inner View occupies whole space from top to bottom and left to right. The flexDirtection: "row" set the elements in row-wise inside the inner View component.
  1. import React, { Component } from "react";  
  2. import { StyleSheet, View, TextInput, Button } from "react-native";  
  3.   
  4. export default class App extends Component {  
  5.     state = {  
  6.         placeName: "",  
  7.         places: []  
  8.     };  
  9.   
  10.     placeNameChangedHandler = val => {  
  11.         this.setState({  
  12.             placeName: val  
  13.         });  
  14.     };  
  15.   
  16.     placeSubmitHandler = () => {  
  17.         alert("button clicked")  
  18.     };  
  19.   
  20.     render() {  
  21.         return (  
  22.             <View style={styles.container}>  
  23.                 <View style={styles.innerContainer}>  
  24.                     <TextInput  
  25.                             placeholder="An awesome place"  
  26.                             onChangeText={this.placeNameChangedHandler}  
  27.                     />  
  28.                     <Button  
  29.                             title="Button"  
  30.                             onPress={this.placeSubmitHandler}  
  31.                     />  
  32.                 </View>  
  33.             </View>  
  34.         );  
  35.     }  
  36. }  
  37.   
  38. const styles = StyleSheet.create({  
  39.     container: {  
  40.         flex: 1,  
  41.         padding: 26,  
  42.         backgroundColor: "#fff",  
  43.         justifyContent: "flex-start"  
  44.     },  
  45.     innerContainer:{  
  46.         flex: 1,  
  47.         flexDirection: "row"  
  48.     }  
  49. });  
Output:
React Native Positioning Element with Flex
The flex: 1 for inner View occupy full space which does not look good as the TextInput and Button occupy all space from top to bottom.

Example 3

In this example, we remove flex property of inner View and add width: 100%. Removing flex form inner View set the default dimension of elements. Setting width :"100%" for inner View occupy full width and default height of elements.
  1. import React, { Component } from "react";  
  2. import { StyleSheet, View, TextInput, Button } from "react-native";  
  3.   
  4. export default class App extends Component {  
  5.     state = {  
  6.         placeName: "",  
  7.         places: []  
  8.     };  
  9.   
  10.     placeNameChangedHandler = val => {  
  11.         this.setState({  
  12.             placeName: val  
  13.         });  
  14.     };  
  15.   
  16.     placeSubmitHandler = () => {  
  17.         alert("button clicked")  
  18.     };  
  19.   
  20.     render() {  
  21.         return (  
  22.             <View style={styles.container}>  
  23.                 <View style={styles.innerContainer}>  
  24.                     <TextInput  
  25.                             placeholder="An awesome place"  
  26.                             onChangeText={this.placeNameChangedHandler}  
  27.                             style={styles.textStyle}  
  28.                     />  
  29.                     <Button  
  30.                             title="Button"  
  31.                             onPress={this.placeSubmitHandler}  
  32.                     />  
  33.                 </View>  
  34.             </View>  
  35.         );  
  36.     }  
  37. }  
  38.   
  39. const styles = StyleSheet.create({  
  40.     container: {  
  41.         flex: 1,  
  42.         padding: 26,  
  43.         backgroundColor: "#fff",  
  44.         justifyContent: "flex-start"  
  45.     },  
  46.     innerContainer:{  
  47.        // flex: 1,  
  48.         width: "100%",  
  49.         flexDirection: "row",  
  50.         justifyContent: "space-between",  
  51.         alignItems: "center"  
  52.     },  
  53.     textStyle:{  
  54.         width: "70%",  
  55.         backgroundColor: "gray",  
  56.     },  
  57.     buttonStyle:{  
  58.         width: "30%",  
  59.     }  
  60. });  
Output:
React Native Positioning Element with Flex

Comments

Popular Posts

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...

16 AWS Gotchas

16 AWS Gotchas In January I launched the MVP for my own startup,  Proximistyle , which helps you find what you’re looking for nearby. On advice from friends and industry contacts I chose AWS as my cloud provider. Having never had to set up my own cloud infrastructure before, the learning curve to get from no experience to a stable VPC system I was happy with was significantly steeper than expected, and had its fair share of surprises. #1 Take advantage of the free resources offered AWS offers a free tier for new accounts. If you have recently bought a domain and set up a company you qualify for the free tier for a year. Additionally, if you are a bootstrapped startup you can apply for  the Startup Builders package  and get $1000 in AWS credits. After doing the above, you’re now ready to get started with setting up the AWS infrastructure for your startup. #2 Set up billing budgets and alerting The very first thing you should do after setting up billing, is enabling a budge...

Ultimate Folder Structure For Your React Native Project

  Ultimate Folder Structure For Your React Native Project React native project structure React Native is a flexible framework, giving developers the freedom to choose their code structure. However, this can be a double-edged sword for beginners. Though it offers ease of coding, it can soon become challenging to manage as your project expands. Thus, a structured folder system can be beneficial in many ways like better organization, simplified module management, adhering to coding practices, and giving a professional touch to your project. This write-up discusses a version of a folder arrangement that I employ in my React Native projects. This structure is based on best practices and can be modified to suit the specific needs of your project. Before we get into the project structure let’s give credit to @sanjay who has the original idea of the structure but I modify his version of the code, to make it better. Base library axios  — For network calling. react-navigation ...