Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(React native) How to use SafeAreaView for Android notch devices?

I'm developing an app with React Native and I'm testing with my OnePlus 6 and it has a notch. The SafeAreaView is a solution for the iPhone X but for Android, it seems there is no solution.

How to solve this kind of issue?

like image 576
Thomas Desdoits Avatar asked Jul 11 '18 15:07

Thomas Desdoits


People also ask

Does SafeAreaView work for Android?

The SafeAreaView is a solution for the iPhone X but for Android, it seems there is no solution.

Is SafeAreaView only for iOS?

The purpose of SafeAreaView is to render content within the safe area boundaries of a device. It is currently only applicable to iOS devices with iOS version 11 or later.

Should I use SafeAreaView in React Native?

You have to only use when any screen has headerMode:none or it out side of the navigation. If you are using full screen modal then you should use <SafeAreaView> . I don't thing having navigation handles it. For android it might but for iOS with swipe gesture to go to home on bottom, one must use safeareview.

Where do I put SafeAreaView?

import { SafeAreaView } from 'react-native'; You just use it in place of the top-level View component. It makes sure content within the safe area boundaries is properly rendered around the nested content and applies padding automatically.


2 Answers

Do something like

import { StyleSheet, Platform, StatusBar } from "react-native";  export default StyleSheet.create({   AndroidSafeArea: {     flex: 1,     backgroundColor: "white",     paddingTop: Platform.OS === "android" ? StatusBar.currentHeight : 0   } }); 

And then In your App.js

import SafeViewAndroid from "./components/SafeViewAndroid";   <SafeAreaView style={SafeViewAndroid.AndroidSafeArea}>           <Layout screenProps={{ navigation: this.props.navigation }} /> //OR whatever you want to render         </SafeAreaView> 

This should work good as get height will take care of the knotch in android device by calculating the statusBar height and it will arrange accordingly.

like image 131
Rishav Kumar Avatar answered Sep 22 '22 23:09

Rishav Kumar


A work around I had to use recently:

GlobalStyles.js:

import { StyleSheet, Platform } from 'react-native'; export default StyleSheet.create({     droidSafeArea: {         flex: 1,         backgroundColor: npLBlue,         paddingTop: Platform.OS === 'android' ? 25 : 0     }, }); 

It is applied like so:

App.js

import GlobalStyles from './GlobalStyles'; import { SafeAreaView } from "react-native";  render() {     return (       <SafeAreaView style={GlobalStyles.droidSafeArea}>           //More controls and such       </SafeAreaView>     );   } } 

You'll probably need to adjust it a bit to fit whatever screen you're working on, but this got my header just below the icon strip at the top.

like image 43
Zexks Marquise Avatar answered Sep 26 '22 23:09

Zexks Marquise