React native provides Dimensions
API and useWindowDimensions()
hook to get width & height size of Mobile Screen and Window, in React Native.
With this, you can calculate the dimensions of Screen and Window.
What is the difference between Window & Screen?
A Window is the space used by the app excluding the status bar at the top and navigation bar at the bottom. While Screen holds the dimension of entire mobile screen.
We can get the dimensions using these commands –
import { Dimensions } from 'react-native'; // Getting Dimensions of Window đ const {width, height} = Dimensions.get('window') // Getting Dimensions of Screen đ const {width, height} = Dimensions.get('screen')
But getting the dimensions using this method is static. Suppose, you are using a foldable device then you need to know when the screen is folded and it’s size changed.
Using the above method will not change the value of width & height on folding of device. Although, if you call Dimensions.get('window')
again then you will get the new values.
To handle this situation, you have 3 choices –
- Call
Dimensions.get('window')
on each render of component. So that if the device is rotated, then the new size will take into effect. - You can use
addEventListener()
to listen to dimensions changes. - Use
useWindowDimensions()
hook.
Using a listener is messy because then you will need to remove the listener also, when the component is unmounted. That’s why we prefer to use useWindowDimensions()
hook. Like this –
import { useWindowDimensions } from 'react-native'; const { height, width } = useWindowDimensions();
This hook will automatically update the values of height & width with the change in app window’s dimensions.
Code Example
import React, { useState, useEffect } from "react"; import { View, StyleSheet, Text, Dimensions, useWindowDimensions } from "react-native"; const App = () => { const windowDimen = useWindowDimensions(); const window = Dimensions.get("window"); const screen = Dimensions.get("screen"); return ( <View style={styles.container}> <View> <Text style={styles.header}> Window Dimensions </Text> <Text>{JSON.stringify(window)}</Text> </View> <View> <Text style={styles.header}> Screen Dimensions </Text> <Text>{JSON.stringify(screen)}</Text> </View> <View> <Text style={styles.header}> useWindowDimensions() Hook </Text> <Text>{JSON.stringify(windowDimen)}</Text> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "space-around", alignItems: "center" }, header: { fontSize: 16, marginVertical: 10 } }); export default App;
Output –
In the above image you can see that the Dimension api works in both Android and iOS phones.
In the code we have used both the useWindowDimensions()
hook as well as Dimensions
api.
Live Demo
React Native Series
Alert
- Basic Alert
- Dismiss on Clicking Outside
- Input Fields in Alert Dialog – Prompt()
- Dark-Light Theme of Dialog
ActivityIndicator
- Basic Circular Loader
- Change size of Circular Loader
- Show/hide Circular Loader
- Change color of Circular Loader
Button
FlatList
- Simple List
- Single Item Selection from List
- Multiple Item Selection from List
- Adding separator between list items
- Multiple columns List
- Showing Message in Empty List
- Add Footer to the List
- Add Header to the List
- Horizontal List
- Inverted List
- Pull to Refresh in List
- Infinite Loading List
SectionList
ScrollView
- ScrollView
- Stick Single Item at Header
- Stick Multiple Items at Header
- Stick Item at Footer
- Hide Sticky Element on Scroll
Image
- Display Image from remote url
- Display local storage image
- Display Base64 Image
- Display Gif & Webp Images
- Adding Blur to Image
- Displaying loader for Image
- Resizemode for Images
- Setting Default Placeholder Image
- Background Image
Modal
RefreshControl
- RefreshControl
- Change Refresh Loader Color
- Change Refresh Loader Size
- Change Refresh Loader Background Color
- Title under Refresh Loader
- Change color of title under refresh loader
StatusBar
- Get StatusBar Size
- Change StatusBar Background Color
- Display StatusBar icons & text in While Color
- Display StatusBar icons & text in Dark Color
- Hiding StatusBar
- Translucent StatusBar
Switch
Text
- Adding Text
- Bold Text
- Italic Text
- Underline Text
- Selecting Text for copy-paste
- Changing Highlight Color of Text Selection
- Fit text in View box
- Clickable anchors in text
- Truncate Lengthy Text
TextInput
- Simple Input Field
- Auto Capitalize Text in Input Field
- Multiline Input Field
- Hide Cursor in Input Field
- Clear input Field using X
- Clear input Field when focused
- Change Cursor Color in Input Field
- Disable input field
- Icon at the left of Input Field
- Dark-Light Keyboard
- Avoid Overlapping of Keyboard
- Limiting Characters in Input Field
- Numeric Keyboard
- Email Id Keyboard
- Phone number Keyboard
- Url Keyboard
- Placeholder in input field
- Placeholder Color in Input Field
- Password Input Field
- Programmatically select text in Input Field
- Change Text Selection Color in Input Field
- Select Whole text in Input Field on Focus
- Write text from center in input field
- Changing underline color of input field
TouchableWithoutFeedback
TouchableHighlight
TouchableOpacity
Pressable
Appearance
AppState
ToastAndroid
Dimensions
Keyboard