I'm trying to set the z order of a UI element (i.e. a View) so that it will overlap another element, but calling ViewGroup.bringChildToFront() has a weird side effect....it moves the element to be the last item in the parent (the ViewGroup). Is this a bug, expected behavior, or what? More importantly, how can I set the z order or a View without this unfortunate side effect?
This is the expected behavior. bringChildToFront() just changes the index of the View inside its parent.
To send a view all the way back, do this:
private void moveToBack(View currentView) {
ViewGroup viewGroup = ((ViewGroup) currentView.getParent());
int index = viewGroup.indexOfChild(currentView);
for(int i = 0; i<index; i++) {
viewGroup.bringChildToFront(viewGroup.getChildAt(i));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With