I have a linear layout which consists of imageview and textview , one below another in a linear layout.
I use this, if you want only one view to be bring to front when needed:
containerView.bringChildToFront(topView);
containerView is container of views to be sorted, topView is view which i want to have as top most in container.
for multiple views to arrange is about to use setChildrenDrawingOrderEnabled(true) and overriding getChildDrawingOrder(int childCount, int i) as mentioned above.
RelativeLayout works the same way, the last image in the relative layout wins.
You can use view.setZ(float)
starting from API level 21. Here you can find more info.
Try this in a RelativeLayout:
ImageView image = new ImageView(this);
image.SetZ(float z);
It works for me.
An alternative is to change the order in which the views are drawn by the parent. You can enable this feature from ViewGroup by calling setChildrenDrawingOrderEnabled(true) and overriding getChildDrawingOrder(int childCount, int i).
/**
* Example Layout that changes draw order of a FrameLayout
*/
public class OrderLayout extends FrameLayout {
private static final int[][] DRAW_ORDERS = new int[][]{
{0, 1, 2},
{2, 1, 0},
{1, 2, 0}
};
private int currentOrder;
public OrderLayout(Context context) {
super(context);
setChildrenDrawingOrderEnabled(true);
}
public void setDrawOrder(int order) {
currentOrder = order;
invalidate();
}
@Override
protected int getChildDrawingOrder(int childCount, int i) {
return DRAW_ORDERS[currentOrder][i];
}
}
Calling OrderLayout#setDrawOrder(int)
with 0-1-2 results in: