React native by default gives us a class named as Dimensions which is used for many work. But in this tutorial we would going to use this class to obtain the Android + iPhone mobile device Height and Width using react native mobile application on button click and show inside Alert Dialog message. So here is the complete step by step tutorial for React Native Get Device Height Width on Button Click programmatically project.
Contents in this project Get Device Height Width on Button Click :
1. Start a fresh React Native project. If you don’t know how then read my this tutorial.
2. Import AppRegistry, StyleSheet, Text, View, Button, Dimensions and Alert component.
1
2
3
|
import React, { Component } from ‘react’;
import { AppRegistry, StyleSheet, Text, View, Button, Dimensions,Alert } from ‘react-native’;
|
3. Create a function named as GetHeightFunction() in your project’s class . This class is used to get height of your Android and iOS mobile phone device using Dimensions.get(‘window’).height method. After getting the height we would save that height into a const variable. Now we would show that height on screen using Alert message.
1
2
3
4
5
6
|
GetHeightFunction = () =>
{
const Height_Holder = Dimensions.get(‘window’).height;
Alert.alert(“Device Height: “ + Height_Holder);
}
|
4. Create another function named as GetWidthFunction() to obtain the width of your device.
1
2
3
4
5
6
|
GetWidthFunction = () =>
{
const Width_Holder = Dimensions.get(‘window’).width;
Alert.alert(“Device Width: “ + Width_Holder);
}
|
5. Create a View as Parent View in render’s return block.
1
2
3
4
|
<View style={styles.MainContainer}>
</View>
|
6. Create 2 children View inside the Parent View. Each children view holds a Button component.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<View style={styles.MainContainer}>
<View style={styles.ButtonStyle}>
</View>
<View style={styles.ButtonStyle}>
</View>
</View>
|
7. Now Create 2 buttons inside each Children View and call the GetHeightFunction() and GetWidthFunction() function on each button onPress.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<View style={styles.MainContainer}>
<View style={styles.ButtonStyle}>
<Button title = “Click Here To Show Device Height” onPress = { this.GetHeightFunction }/>
</View>
<View style={styles.ButtonStyle}>
<Button title = “Click Here To Show Device Width” onPress = { this.GetWidthFunction } />
</View>
</View>
|
8. Create custom Style sheet class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
backgroundColor: ‘#F5FCFF’,
margin: 10,
},
ButtonStyle: {
margin: 10
}
});
|
9. Complete source code for index.android.js / index.ios.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import React, { Component } from ‘react’;
import { AppRegistry, StyleSheet, Text, View, Button, Dimensions,Alert } from ‘react-native’;
export default class Project extends Component {
GetHeightFunction = () =>
{
const Height_Holder = Dimensions.get(‘window’).height;
Alert.alert(“Device Height: “ + Height_Holder);
}
GetWidthFunction = () =>
{
const Width_Holder = Dimensions.get(‘window’).width;
Alert.alert(“Device Width: “ + Width_Holder);
}
render() {
return (
<View style={styles.MainContainer}>
<View style={styles.ButtonStyle}>
<Button title = “Click Here To Show Device Height” onPress = { this.GetHeightFunction }/>
</View>
<View style={styles.ButtonStyle}>
<Button title = “Click Here To Show Device Width” onPress = { this.GetWidthFunction } />
</View>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
backgroundColor: ‘#F5FCFF’,
margin: 10,
},
ButtonStyle: {
margin: 10
}
});
AppRegistry.registerComponent(‘Project’, () => Project);
|
Screenshot in iOS mobile phone :
Screenshot in Android mobile phone device: