Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] position: absolute shouldn't be cut off within a container with border #12534

Open
linmic opened this issue Feb 23, 2017 · 70 comments
Open
Labels
Bug Never gets stale Prevent those issues and PRs from getting stale Platform: Android Android applications. Ran Commands One of our bots successfully processed a command.

Comments

@linmic
Copy link
Contributor

linmic commented Feb 23, 2017

Description

If you have an element position: absolute with negative offset within a container with a border, it's going to be cut off, it's like overflow: hidden of the container is suddenly enabled by the border. But if you remove the border of the container, it works well. This issue only happens on Android.

#3198 is a similar issue, and was reported with lots of discussions. However, I believe they are not exactly the same.

Reproduction

I've made an example on rnplay to reproduce the issue.

Solution

TBD

Additional Information

  • React Native version: 0.33 (rnplay), 0.39 (the version I currently use)
  • Platform: Android
  • Operating System: MacOS
@linmic linmic changed the title [Android] position: absolute shouldn't be overflow: hidden within a container with border [Android] position: absolute shouldn't be cut off within a container with border Feb 23, 2017
@jakallergis
Copy link

Same here. This is really frustrating

@kylanhurt
Copy link

Yeah this is currently happening with me even when the containing view has no border nor border-radius. This is definitely an issue because, for example, I want to show a circular icon on the top + middle area of my modal window. You can see it getting cut off below:

image

@renso3x
Copy link

renso3x commented Jun 14, 2017

Any solution for this issue? I have also experience it on android development.

@olliekav
Copy link

I can't find any way around this, guess we are just stuck with different styling for Android.

@jinqiupeter
Copy link

Same issue here, view with absolute positioning and negative top works on iOS but got cut off on Android

@scriptfans
Copy link

  • 1 for this, how to fix it?

@brunolemos
Copy link
Contributor

brunolemos commented Sep 11, 2017

Facing this as well :(

image

Works great on iOS:

image

@aforty
Copy link
Contributor

aforty commented Sep 19, 2017

This is an issue for me as well.

@brunolemos
Copy link
Contributor

@grabbou @bvaughn know any android dev that could help us with this bug?

@yelkamel
Copy link

yelkamel commented Sep 20, 2017

If some have a solution for that it's should be great.
My java android skill is not good enough to make a PR :(

@Maximell
Copy link

Maximell commented Oct 8, 2017

+1 also having this issue.

@felipap
Copy link

felipap commented Oct 21, 2017

Has been a big problem here too. Anybody found a fix?

@chrispcode
Copy link

Also having this problem with Android, neither overflow: hidden, nor zIndex: 1000 seem to fix the problem!

@mtin79
Copy link

mtin79 commented Nov 28, 2017

Same here. Needs a fix!
What are you doing to positioning elements in relationship to another one?
E.g. for tooltips etc.?

@designorant
Copy link
Contributor

designorant commented Nov 28, 2017

Workaround

Use an extra container and fake the overflow with it:
https://snack.expo.io/@designorant/overflow-visible-android-workaround

import React, { Component } from "react";
import { View, Text, StyleSheet } from "react-native";

export default class App extends Component {
  render() {
    return (
      <View style={styles.mainContainer}>

        <View style={styles.componentContainer}>
          <View style={[styles.element, styles.element1]} />
          <Text style={styles.text}>overflow: "visible"</Text>
        </View>

        <View style={styles.extraComponentContainer}>
          <View style={[styles.element, styles.element2]} />
          <View style={styles.componentContainer}>
            <Text style={styles.text}>extra container workaround</Text>
          </View>
        </View>

      </View>
    );
  }
}

const styles = StyleSheet.create({
  mainContainer: {
    flex: 1,
    alignItems: "center",
    justifyContent: "space-around"
  },
  componentContainer: {
    backgroundColor: "#d3d3d3",
    borderWidth: 1,
    borderColor: "grey",
    width: 200,
    height: 200,
    position: "relative",
    overflow: "visible" // doesn't do anything
  },
  extraComponentContainer: {
    // fakes overflow but requires more markup
    backgroundColor: "#ffff00",
    paddingTop: 20,
    paddingLeft: 20,
    paddingBottom: 20,
    paddingRight: 20,
    position: "relative"
  },
  element: {
    backgroundColor: "#ff0000",
    width: 40,
    height: 40
  },
  element1: {
    position: "absolute",
    top: -20,
    left: -20
  },
  element2: {
    position: "absolute",
    top: 0,
    left: 0,
    zIndex: 100,
    elevation: 100
  },
  text: {
    textAlign: "right",
    backgroundColor: "transparent"
  }
});

overflowvisible

@mtin79
Copy link

mtin79 commented Nov 29, 2017

@designorant Thanks Michal for your advice!
One question remains: Wouldn't the "extraComponentContainer" prevent clicks/actions/events to elements/components below it?

I know in css there is "pointer-events". When its set to "none" (e.g. pointer-events: none; ) The elements lets all events pass through to other elements below it.
Anything like that in react-native?

@brunolemos
Copy link
Contributor

Yes, react native has the pointerEvents prop.

@stefanedberg
Copy link

stefanedberg commented Dec 21, 2017

I have this problem too in rn 0.51.0.

@kyoyadmoon
Copy link

kyoyadmoon commented Jan 5, 2018

after some testing, it also happened when container has these styles

  • backgroundColor
  • borderColor
  • zIndex

@MorganIsBatman
Copy link

Thank you @brunolemos !! I had so much stupid layouts to try and get this to work. A full page 'container' View with pointerEvents='none' was all I needed!

@jpudysz
Copy link

jpudysz commented Jan 15, 2018

@brunolemos thanks! We did manage to fix out issue with poitnerEvents="box-none" and position absolute

@shettayyy
Copy link

      <View style={ styles.container }>
        <TouchableOpacity style={ styles.menuButton } onPress={ this.toggleMenu }>
          <Ionicons name="ios-menu" size={ 32 } color="#fff" />
        </TouchableOpacity>

        <Text style={ styles.headerTitle }>VERUS</Text>

        <View style={ styles.menu } pointerEvents="box-none">
          <Fade visible={ this.state.menuVisible }>
            { menuItems.map( this.renderMenuitem ) }
          </Fade>
        </View>
      </View>
import EStyleSheet from 'react-native-extended-stylesheet';

const styles = EStyleSheet.create( {
  $headerHeight: 50,

  container: {
    flexDirection: 'row',
    alignItems: 'center',
    // backgroundColor: '#0050aa',
    paddingHorizontal: 10,
    height: '$headerHeight',
    position: 'relative',
  },

  menuButton: {
    marginTop: 3,
  },

  headerTitle: {
    marginLeft: 10,
    color: '#fff',
    fontSize: 16,
    fontWeight: '700',
  },

  menu: {
    position: 'absolute',
    top: '$headerHeight',
    left: 10,
  },

  menuItem: {
    padding: 10,
    backgroundColor: '#0050aa',
    borderBottomWidth: 1,
    borderBottomColor: '#eee',
  },

  menuItemName: {
    color: '#fff',
    fontWeight: '700',
  },
} );

export default styles;

I am still facing this issue. If I do not add backgroundColor to the container, then it works perfectly fine. It only happens on Android.

@ifours
Copy link

ifours commented Mar 5, 2018

To overcome this issue you can render a absolute positioned view with your background color on Android. Following code will work on android :)

      <View style={{ flex: 1, backgroundColor: 'red' }}>
        <View style={{ height: 200, backgroundColor: 'green' }} />
        <View style={{ height: 200 }}>
          <View style={{ position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, backgroundColor: 'yellow' }} />
          <View style={{ height: 50, width: 50, backgroundColor: 'aqua', position: 'absolute', top: -20 }} />
        </View>
      </View>

@salujaharkirat
Copy link

I was working on an order summary page in which I need to show all orders made by user. Facing similar issue on android. Tried workaround @designorant suggested and it works fine.
Here is the sample code if it helps anyone :)
https://snack.expo.io/@harkiratsaluja/timeline

@shettayyy
Copy link

Did anyone find a fix? This issue still exists. I am trying to create a hamburger menu (vertical dropdown) and the container with menu items gets clipped inside the header rendered by react-navigation. This only happens on Android.

@react-native-bot react-native-bot added Android Ran Commands One of our bots successfully processed a command. labels Mar 18, 2018
@saadq
Copy link

saadq commented Dec 9, 2019

I don't think this should be closed yet :|

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 9, 2019
@davimattos
Copy link

I fix it setting a elevation together the position.

elevation : 10, position: "absolute"

@stale
Copy link

stale bot commented Mar 9, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 9, 2020
@thinklinux
Copy link

I don't think this should be closed yet. Tho there is a workaround with the elevation property. Unless it is not a workaround but the correct way of doing it which is not documented good enough.

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 9, 2020
@davidecarpini
Copy link

I resolved using react-native Modal as wrapper:
https://reactnative.dev/docs/modal

@fabOnReact
Copy link
Contributor

can not find the reproducible example.

@fabOnReact
Copy link
Contributor

@ravirajn22
Copy link

I quote Is it a bad practice to use negative margins in Android?

image

Actually what android does should not matter because react-native does not use any android layout system, they use flexbox implemented using yoga.

@stale
Copy link

stale bot commented Dec 25, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 25, 2020
@pinchez254
Copy link

Using StyleSheet from react-native and adding elevation key to the style props fixes the issue ..
const styles = StyleSheet.create({ elevatedBox :{ flex: 1, height: 100, width: 280, position: 'absolute', top: 160 elevation: 5 // <----- add this })

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Feb 21, 2021
@raijal
Copy link

raijal commented Feb 14, 2022

Try This Hope This works. Just play around the and Styles.

Your content goes here );

const styles = StyleSheet.create({
profileContainer: {
width: "100%",
flex: 1,
backgroundColor: "#092442",
alignItems: "center",
},
profile: {
position: "absolute",
zIndex: 1,
},
userProfileImg: {
height: 80,
width: 80,
borderRadius: 40,
},
profileContent: {
backgroundColor: "#fff",
flex: 1,
marginTop: "5%",
width: "100%",
borderRadius: 8,
},
});

@iway1
Copy link

iway1 commented Aug 25, 2022

@fabriziobertoglio1987 does this count as negative margins? it's absolute positioning, not margin?

And I can concur, negative margins will break your layouts across android devices in React Native, I've seen it happen in prod =)

@fabOnReact
Copy link
Contributor

fabOnReact commented Oct 24, 2022

@fabriziobertoglio1987 does this count as negative margins? it's absolute positioning, not margin?

And I can concur, negative margins will break your layouts across android devices in React Native, I've seen it happen in prod =)

@iway1 as explained by @sibelius, It is caused by ViewGroup#setClipChildren

Read more at https://medium.com/entria/solving-view-overflow-in-android-reactnative-f961752a75cd

@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Apr 23, 2023
@SimpleCreations
Copy link

Still an issue

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Apr 23, 2023
@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 21, 2023
@SimpleCreations
Copy link

Still an issue

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 23, 2023
@cortinico cortinico added the Never gets stale Prevent those issues and PRs from getting stale label Oct 24, 2023
@fabOnReact
Copy link
Contributor

fabOnReact commented Jan 18, 2024

#12534 (comment)

@fabOnReact
Copy link
Contributor

This issue does NOT reproduce on the react-native main branch.

I quote the readme from https://github.com/entria/react-native-view-overflow (the package was published to fix this issue).

This library will be useful for React Native versions below v0.57. For v0.57 and beyond, React Native supports Android overflow natively from this commit.

CLICK TO OPEN TESTS RESULTS - BEFORE

Test case from issuecomment-347648303.

CLICK TO OPEN TESTS RESULTS - AFTER

Screenshot 2024-02-01 at 5 57 18 PM

cc @cortinico @lunaleaps

@Ha0ran2001
Copy link

When I use react-native-linear-gradient element as father container, set negative top value doesn't work but use View as relative element works fine.
change

<LinearGradient
                colors={['#F8E2C4', '#F3BB6C']}
                start={{x: 0, y: 0}}
                end={{x: 1, y: -1}}
                style={{
                  borderRadius: 20,
                  position: 'relative',
                  paddingVertical: 3.2 * vw,
                  paddingHorizontal: 5.33 * vw,
                }}
              >
                <Text
                  style={{
                    fontSize: 4 * vw,
                    lineHeight: 4 * vw,
                    fontWeight: '500',
                  }}
                >
                  {rightCardButtonText}
                </Text>
                {rightCardSubTitle && (
                  <LinearGradient
                    colors={['#F1933C', '#F13C3C']}
                    start={{x: -0.31, y: 0}}
                    end={{x: 0.95, y: 1}}
                    style={{
                      position: 'absolute',
                      top: '-28%',
                      right: 0,
                      borderTopLeftRadius: 16,
                      borderTopRightRadius: 20,
                      borderBottomRightRadius: 20,
                      borderBottomLeftRadius: 1,
                      paddingVertical: 0.53 * vw,
                      paddingHorizontal: 1.6 * vw,
                    }}
                  >
                    <Text
                      style={{
                        fontWeight: 'bold',
                        fontSize: 10,
                        fontFamily: 'ZHVipFont',
                      }}
                    >
                      {rightCardSubTitle}
                    </Text>
                  </LinearGradient>
                )}
              </LinearGradient>

to

<View
                style={{
                  borderRadius: 20,
                  position: 'relative',
                  paddingVertical: 3.2 * vw,
                  paddingHorizontal: 5.33 * vw,
                }}
              >
                <Text
                  style={{
                    fontSize: 4 * vw,
                    lineHeight: 4 * vw,
                    fontWeight: '500',
                  }}
                >
                  {rightCardButtonText}
                </Text>
                {rightCardSubTitle && (
                  <LinearGradient
                    colors={['#F1933C', '#F13C3C']}
                    start={{x: -0.31, y: 0}}
                    end={{x: 0.95, y: 1}}
                    style={{
                      position: 'absolute',
                      top: '-28%',
                      right: 0,
                      borderTopLeftRadius: 16,
                      borderTopRightRadius: 20,
                      borderBottomRightRadius: 20,
                      borderBottomLeftRadius: 1,
                      paddingVertical: 0.53 * vw,
                      paddingHorizontal: 1.6 * vw,
                    }}
                  >
                    <Text
                      style={{
                        fontWeight: 'bold',
                        fontSize: 10,
                        fontFamily: 'ZHVipFont',
                      }}
                    >
                      {rightCardSubTitle}
                    </Text>
                  </LinearGradient>
                )}
              </View>

it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Never gets stale Prevent those issues and PRs from getting stale Platform: Android Android applications. Ran Commands One of our bots successfully processed a command.
Projects
None yet
Development

No branches or pull requests