zIndex does not work with dynamic components on Android
I am trying to render elements conditionally where each element has different zIndex style property. Using folowing code in Android emulator with react-native 0.30.0.
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default class Demo extends Component {
constructor(props, ctx) {
super(props, ctx);
this.state = {
showGreen: true,
};
}
render() {
return (
<View style={{flex: 1, padding: 20}}>
<View style={[styles.item, {zIndex: 3, backgroundColor: 'red'}]}>
<Text>zIndex: 3</Text>
</View>
{this.state.showGreen &&
<View style={[styles.item, {zIndex: 2, backgroundColor: 'green'}]}>
<Text>zIndex: 2</Text>
</View>
}
<View style={[styles.item, {zIndex: 1, backgroundColor: 'blue'}]}>
<Text>zIndex: 1</Text>
</View>
<View style={styles.button}>
<Text onPress={() => this.setState({ showGreen: !this.state.showGreen })}>
Toggle green
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
item: {
marginTop: -20,
height: 50,
paddingTop: 22,
},
button: {
backgroundColor: 'gray',
marginTop: 30,
}
});
Initial screen looks like expected:

When I click 'Toggle green' button to dynamically show/hide green element it hides blue element instead and furthermore element's text is missing:

When I click the button again, red and green elements remains visible and the toggle button jumps down.
same here, zIndex + position : absolute, any update ?
Instead of return null I just return <View /> and it works fine for me on both Android and iOS.
any updates on this? I am also experiencing this bug, with zIndex + position : absolute on android
Created product pain: https://react-native.canny.io/feature-requests/p/zindex--positionabsolute-breaks-with-dynamic-components-on-android
If the title and/or post can be clarified, I'm all for it.
Same issue here 🤣
Experiencing the same issue here, but even with position: relative. I was able to fix this by using a Modal component, but I need the rest of the screen to be touchable, and Modals are fullscreen so I could not use that either. Thanks!
I am using Modal from react-native and when I close it (set visible to false), it makes my other components disappear one by one. First close navbar, second close a card. Both has elevation and zIndex set.
@rnowm Can you share your fix with Modal?
Glad I'm not the only one having issues with this (zIndex + position: absolute). I've been trying to statically set zIndex's, but then realized I could just ditch zIndexing my absolute views and just reorder them in my render return so that they stay on top. I know this will not fix most of the problems of those who actually need to use zIndex but hopefully it helps someone.
@Gumija in my case I just needed one component with z-index (a toast) over my view, maybe that's why it worked for me. Did not tested with other elements using z-index.
Same issue here
I'm also having an issue with zIndex + position: 'absolute' on Android. I have a dropdown menu component that always displays the currently selected item, and when it is activated, it also displays a list of all of the available dropdown menu items below this. The view that this dropdown menu is used in has other components as siblings of the dropdown menu component, and they are all after the dropdown menu component, since I want the currently selected item of the dropdown menu component to be shown first.
I'm trying to use zIndex so that the list of the available dropdown menu items is shown above the other components that are rendered after the dropdown menu. I'm simply applying a zIndex: 2 to the <View> that contains this list of available dropdown menu items to achieve this. The list of available dropdown menu items is rendered within a <View> that has position: 'absolute'.
This technique is working perfectly on iOS and behaves exactly as I expect/want. However, the exact same code is not working on Android; The list of available dropdown menu items is always rendered behind the other components that are rendered after the dropdown menu, and therefore is not visible. I've tried all sorts of variations of zIndex values for different components in the hierarchy, as well as elevation, and nothing seems to make this work on Android, even though it works fine on iOS.
Something must be different about how zIndex works in iOS vs Android, as the same code behaves differently in these two environments.
Same here.
Problem's still here
same here.
Adding "elevation: 100" to the style solved my issue. credit goes to Shukarullah Shah
Same issue here, everything works as expected without a zIndex (It just doesn't look right) Once I add a zIndex of 1 it goes to shit.
One tip that might be helpful: You might get away with changing the rendering order instead of using zIndex. This only works on elements under the same parent though.
i got same issue on android, add loading component with absolute + zIndex. It will cause unexpected rendering on that screen (such as some components disappear or white out all screen or some components) and stop the app from working. I have tried all solution mention above such add elevation: 100 and move loading component to the last component in the view. But the issue still happens on android.
https://gist.github.com/nhuthuynh/009dee73675c1dbed41d34a86b359057
"react": "^16.0.0-alpha.6",
"react-native": "^0.43.4",
Genymotion: 2.9.0
Virtual Device: Google Nexus 5X 1080x1920
Got the same problem here ! After reading the discussions and skimming through the code I realized that the root problem lies in the current implementation of zIndex which is basically invoking bringToFront() on the child views in order of their zIndex. ( It is done in ViewGroupManager.reorderChildrenByZIndex() method)
This method spoils the natural index of child views in their parent. Therefore indicesToRemove parameter in NativeViewHierarchyManager.manageChildren will no longer refer to the actual child.
@asgvard did a beautiful job in PRs (#10954) trying to use tag-reference instead of child-index. However @astreet suggested the problem is better be solved fundamentally by migrating to a zIndex implementation mechanism that doesn't mess with child-view indices (probably using android's ViewGroup#getChildDrawingOrder).
It seems like 23 days ago @janicduplessis fixed zIndex mechanism in commit 9a51fa8. When I used react-native source from master branch everything was working just fine.
Hope this commit goes on the next release !
actually, elevation property worked on android, just remove zIndex property on android platform and make the component with elevation property is the last component.
ios: {
zIndex: 1
},
android: {
elevation: 1
},
it works for me! thank all!