showing results for - "getting view height dynamically in react native"
Gianluca
04 Jul 2019
1import React, { Component } from 'react';
2import ReactNative, { AppRegistry, View, TouchableOpacity, findNodeHandle, Text, StyleSheet } from 'react-native';
3
4
5export default class App extends Component {
6
7  constructor() {
8    super();
9
10    this.state = { x: null, y: null, width: null, height: null }
11  }
12
13  calculateDimensions = () => {
14    this.refs.innerView.measureLayout(ReactNative.findNodeHandle(this.refs.containerView), (xPos, yPos, Width, Height) => {
15      this.setState({ x: xPos, y: yPos, width: Width, height: Height });
16    });
17  }
18
19  render() {
20    return (
21      <View ref="containerView" style={styles.container}>
22        <View ref="innerView" style={styles.innerView}>
23          <Text style={styles.text}>X: {this.state.x}</Text>
24          <Text style={styles.text}>Y: {this.state.y}</Text>
25          <Text style={styles.text}>Width: {this.state.width}</Text>
26          <Text style={styles.text}>Height: {this.state.height}</Text>
27        </View>
28
29        <TouchableOpacity activeOpacity={0.8} style={styles.ButtonDesign} onPress={this.calculateDimensions}>
30          <Text style={styles.text}>Calculate Blue Box's Dimensions</Text>
31        </TouchableOpacity>
32      </View>
33    );
34  }
35}
36
37const styles = StyleSheet.create(
38  {
39    container: {
40      flex: 1,
41      justifyContent: "center",
42      alignItems: "center",
43
44    },
45    innerView: {
46      backgroundColor: '#ff5722',
47      width: '70%',
48      height: '70%',
49      marginBottom: 50,
50      justifyContent: 'center',
51      alignItems: 'center'
52    },
53    ButtonDesign: {
54      position: 'absolute',
55      left: 0,
56      right: 0,
57      bottom: 0,
58      paddingHorizontal: 10,
59      height: 50,
60      justifyContent: 'center',
61      alignItems: 'center',
62      backgroundColor: '#0091ea',
63    },
64    text: {
65      color: 'white',
66      fontSize: 17
67    },
68
69  });
queries leading to this page
react native dyamic vew sizedynamic height react nativeget dynamic height of view react nativehow to set view height programmatically in react natviedynamically set width of view react nativereact native view height valuesreact native view dynamic widthreact native change height dynamicallyhow to set the dynamic width of view in react nativehow to increase height of view in react nativereact native dynamically get parent sizeim hiding view bit it is taking width and height react nativehow to get view height in react nativereact native set view heightdynamic height of view react nativereact native get view heightreact native webview dynamic heightreact native view dynamic heightgetting view height dynamically in react nativedynamically changing height of react native webviewdynamic height of view reactnativemesure width and height dynmically in react nativehow to get height of dynamic component react nativeget width of view in react nativehow to get height of view in react nativehow to calculate view height in react nativeview height react nativereact native calculate view heightview height not changing react nativereact native dynamic height webviewreact native get elements dimensions dynamicallyhow to make view take available height in react nativecalculate dynamic height in react native for a viewget width of view comoponent before it is rendered in react nativereact native get height of componenthow to get height of react native componentgetting view height dynamically in react native