Search code examples
androidanimationviewz-order

Unable to change z order for animated view


I have tried to solve this problem for several hours now, but I totally no idea what to do.

I have a LinearLayout with two RelativeLayouts, each the same height. The bottom ones visibility is "gone" before the animation starts and I want it to "slide out" of the top one onClick. The slide works perfectly, but the animated view is ALWAYS on top of the other one, no matter what I do. As it should look like the bottom one is "sliding out", that is not the effect I want to achieve.

My animation looks like this:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromYDelta="-100%"
    android:interpolator="@android:anim/linear_interpolator"
    android:toYDelta="0"
    />

I have already tried to do view.bringToFront() with the view that is not animated and should just stay on top of the animated one. But as a result the not animated view is displayed as the bottom view.

Can anybody please help me?


Solution

  • This is supported in layout's like Relative layout and framelayout where z-index placement of children views are supported. You can try:

    view.bringToFront(); 
    

    Explanation from docs: Change the view's z order in the tree, so it's on top of other sibling views.

    You can also try:

    bringChildToFront(View child)
    

    Change the z order of the child so it's on top of all other children.

    These methods change the children views in the view tree. appropriate place to do it would be on animationEnded method of your animation.