Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting z order of View with bringChildToFront()

Tags:

android

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?

like image 458
rob Avatar asked Sep 18 '10 02:09

rob


2 Answers

This is the expected behavior. bringChildToFront() just changes the index of the View inside its parent.

like image 98
Romain Guy Avatar answered Nov 19 '22 05:11

Romain Guy


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));
    }
}
like image 29
Kumar Avatar answered Nov 19 '22 04:11

Kumar