Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View bring to front doesnt work

I want to change z order of some views during animation

On Androids above 4.1.2 it works just fine, and on androids below 4.1.2 the Z order doesnt change, the top view remains on top.

This is what i am trying.

myView.bringToFront();
((View)myView.getParent()).invalidate();

How to make it work on older devices ?

like image 438
Lena Bru Avatar asked Aug 14 '14 10:08

Lena Bru


2 Answers

   /**
     * Change the view's z order in the tree, so it's on top of other sibling
     * views. This ordering change may affect layout, if the parent container
     * uses an order-dependent layout scheme (e.g., LinearLayout). Prior
     * to {@link android.os.Build.VERSION_CODES#KITKAT} this
     * method should be followed by calls to {@link #requestLayout()} and
     * {@link View#invalidate()} on the view's parent to force the parent to redraw
     * with the new child ordering.
     *
     * @see ViewGroup#bringChildToFront(View)
     */
    public void bringToFront() {
        if (mParent != null) {
            mParent.bringChildToFront(this);
        }
    }

according to this I was simply missing the line

((View)myView.getParent()).requestLayout();

and it worked!

like image 77
Lena Bru Avatar answered Dec 03 '22 07:12

Lena Bru


I tried all that. A RelativeLayout was supposed to be on top of a Button but it just didn't want to obey.

In the end I solved it by adding android:elevation="2dp" to the RelativeLayout.

The elevation value is only used in API level 21 and higher. In my case everything below the API level 21 was fine and everything above wasn't.

Hope this might help someone :)

like image 22
Colibri Avatar answered Dec 03 '22 06:12

Colibri