The Container View is the main view which would hold all the components in react native application. This View has Flex :1 which would automatically allows it to cover the complete device screen area. It is like the entire body of application like we did see in HTML body tag. We can set its background color, border and layout type. So in this tutorial we would going to create a react native application to Add Show Border Around Root Main Container View in Android iOS app example tutorial. We would use borderColor and borderWidth attributes using custom style sheet to add border.
Contents in Add Show Border Around Root Main Container View in React Native Android iOS App:
1. Import StyleSheet, View, Text and Platform component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, View, Text, Platform} from ‘react-native’;
|
2. Create the Root Container View in render’s return block. We would simply adding a Text component in View just to show some text.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
render() {
return (
<View style={styles.MainContainer}>
{/* Put Your Components Here Inside The Root View. */}
<Text style={{textAlign: ‘center’, fontSize: 20}}> Root Main View With Border </Text>
</View>
);
}
|
3. Create Style for View.
borderWidth : Setting up the border width in pixels.
borderColor : Setting up the complete border color.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
const styles = StyleSheet.create({
MainContainer :
{
flex:1,
padding: 10,
justifyContent: ‘center’,
alignItems: ‘center’,
borderWidth: 5,
borderColor: ‘#FF1744’,
marginTop: (Platform.OS === ‘ios’) ? 20 : 0
}
});
|
4. Complete source code for App.js File :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import React, { Component } from ‘react’;
import { StyleSheet, View, Text, Platform} from ‘react-native’;
export default class Mynewproject extends Component {
render() {
return (
<View style={styles.MainContainer}>
{/* Put Your Components Here Inside The Root View. */}
<Text style={{textAlign: ‘center’, fontSize: 20}}> Root Main View With Border </Text>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :
{
flex:1,
padding: 10,
justifyContent: ‘center’,
alignItems: ‘center’,
borderWidth: 5,
borderColor: ‘#FF1744’,
marginTop: (Platform.OS === ‘ios’) ? 20 : 0
}
});
|
Screenshot in Android device :
Screenshot in iOS device :