13

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?

rob
  • 9,933
  • 7
  • 42
  • 73

3 Answers3

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));
    }
}
GreyCat
  • 16,622
  • 18
  • 74
  • 112
Kumar
  • 151
  • 1
  • 2
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

Hussain
  • 5,552
  • 4
  • 40
  • 50
sm abbas
  • 1,008
  • 12
  • 13