react-native-safe-area-context icon indicating copy to clipboard operation
react-native-safe-area-context copied to clipboard

Does SafeAreaView support android?

Open VictorioMolina opened this issue 4 years ago • 5 comments

I am testing SafeAreaView but this seems not to work on Android.

My app has a minimalistic design and I have hidden the status bar... So, that is why I wrapped most of my components inside your component... The problem is that I have had to write an extra padding for Android like this:

import React from "react"; import { StyleSheet, Platform, StatusBar } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context";

export default function (props) {
  return (
    <SafeAreaView style={styles.AndroidSafeArea} {...props}>
      {props.children}
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  AndroidSafeArea: {
    paddingTop: Platform.OS === "android" ? StatusBar.currentHeight : 0,
  },
});

Something that doesn't seems really elegant.

Will this feature be implemented in a future so that I don't have to use this custom component?

Thank you.

VictorioMolina avatar Nov 09 '20 10:11 VictorioMolina

It works on android for sure. Are you sure you haven't imported SafeAreaView from 'react-native' ?

kuasha420 avatar Nov 13 '20 07:11 kuasha420

Yeah! I am using the one from react-native-safe-area-context. The thing is, that if I don't apply the paddingTop: StatusBar.currentHeight on android, my Samsung Galaxy S20 camera covers the header of my screen.

Pd: I have hidden the status bar for the whole app.

VictorioMolina avatar Nov 13 '20 12:11 VictorioMolina

@VictorioMolina were you able to solve this? I'm dealing with a similar problem on the Pixel 4A.

nickelbob avatar Dec 24 '20 02:12 nickelbob

@nickelbob nope, I didn't :/

VictorioMolina avatar Dec 31 '20 00:12 VictorioMolina

@nickelbob i solved this by follows, you can have a try:

import React from 'react';
import {
  SafeAreaView,
  NativeSafeAreaViewProps,
  useSafeAreaInsets,
  EdgeInsets,
  Edge,
} from 'react-native-safe-area-context';
import styled, { css } from 'styled-components';

const SafeAreaViewStyled = styled(SafeAreaView)<{
  insets: EdgeInsets;
  edges: readonly Edge[];
  mode: 'padding' | 'margin';
}>`
  flex: 1;
  ${(p) =>
    p.edges.map(
      (direction) => css`
        ${p.mode}-${direction}: ${p.insets[direction]}px;
      `,
    )}
  background: #fff;
`;

export const CustomSafeAreaView: React.FC<NativeSafeAreaViewProps> = ({
  children,
  edges = ['top', 'right', 'bottom', 'left'],
  ...restProps
}) => {
  const insets = useSafeAreaInsets();

  return (
    <SafeAreaViewStyled edges={edges} insets={insets} mode="padding" {...restProps}>
      {children}
    </SafeAreaViewStyled>
  );
};


jason-rb2 avatar Jul 22 '21 03:07 jason-rb2

It supports Android

jacobp100 avatar Jan 19 '23 16:01 jacobp100

I had a similar issue when running on Android. If I add the flex prop, it would crash android completely, without any obvious error:

<SafeAreaView flex>

But using the style property works:

<SafeAreaView style={{flex: 1}}>

I would assume that it is due to some kind of property type validation.

rdougan avatar Apr 06 '23 20:04 rdougan