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?
Asked
Active
Viewed 3.0k times
3 Answers
20
This is the expected behavior. bringChildToFront() just changes the index of the View inside its parent.

Romain Guy
- 97,993
- 18
- 219
- 200
15
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));
}
}
-
7"i" instead of "0" at `vg.bringChildToFront(vg.getChildAt(0));` – Jorge Gil Mar 22 '13 at 00:00
0
bringChildToFront(child)
does nothing but changes the index value of the child.
In order to bring a child to the front without using showNext()
or showprevious()
,
use
setDisplayedChild()
and indexOfChild()
together.
example
vf.setDisplayedChild(vf.indexOfChild(child));
where child is the view that needs to be brought front.
Thanks