25

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 ?

Lena Bru
  • 13,521
  • 11
  • 61
  • 126
  • 1
    You are correct, I edited this post from a different question i wanted to ask, and decided not to, the title didnt seem to have changed, fixed it, thanks – Lena Bru Aug 14 '14 at 10:53

6 Answers6

30
   /**
     * 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!

Lena Bru
  • 13,521
  • 11
  • 61
  • 126
  • 2
    Based in your response I do: myView.bringToFront(); myView.requestLayout(); And works also in Android 4.1.1 – Gilian Apr 20 '16 at 18:18
  • 1
    Confirmed while testing on sdkVersion 16 :). I didn't noticed as I was working previously on sdkVersion25. – Juan Mendez Apr 04 '17 at 05:20
17

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 :)

Colibri
  • 713
  • 12
  • 16
6

I hope this will be useful for somebody. None of the above solutions worked for me. Why? Because my view that I wanted to be in the frond had an elevation smaller than other view. So the view with bigger elevation was always in front, no matter what.

Adrian Buciuman
  • 185
  • 3
  • 13
2

If the parent view is a relativelayout, then it might work or it might not work. I tried bringToFront, requestLayout, and removeView; addView, but no luck. My solution was to put everything inside a framelayout. What I needed on top was moved the buttom of the framelayout with visibility invisible, and then in code it was made visible.

kjoelbro
  • 6,296
  • 4
  • 21
  • 18
2

For Api's 21 or above There is an xml attribute known as translateZ which lets you define the elevation level on the view.

You can pretty much use that too.

android:translateZ="30dp"

*APIs >=21

Tilak Raj
  • 1,369
  • 5
  • 31
  • 64
0

According to this I was simply missing the line

This Simple line work for you

yourView.bringToFront();
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103