How to send view to back ? How to control the z-order programmatically?

Lukap picture Lukap · Jul 20, 2011 · Viewed 85.9k times · Source

I have a problem to send the view to back. In Android we have a method like bringToFront(), to place the view on top of the another view. Like that, I want to put the view on below the previous image.

Is there any method like sendToBack() or bringToBack() in Android. If so, can any one help me in this.

Note: that I do not want to control the z-order by the order of placing items in layout I want to control the z-order programmatically.

I do not want to hide the views on the front I just want them to be behind the view that is moving.

Answer

Turix picture Turix · Nov 9, 2013

I realize that this has been implied in other answers, but no one posted the code. I keep the following in a utility class where I have "helper" functions for dealing with views:

public static void sendViewToBack(final View child) {
    final ViewGroup parent = (ViewGroup)child.getParent();
    if (null != parent) {
        parent.removeView(child);
        parent.addView(child, 0);
    }
}