1

I have a FrameLayout within another one. It is aligned to the center.

No I want to align it to top by changing it gravity, but I also want to animate this change.

I already found this answer here https://stackoverflow.com/a/33192335/408780, but LayoutTransition.CHANGING using there needs API 16.

Our app's however minSdk is 14. Any suggestions how to animate gravity change?

Thank you in advance

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/bg"
        android:scaleType="centerCrop"/>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:animateLayoutChanges="true">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/container_bg"/>
        <EditText
            android:layout_gravity="center_vertical"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </FrameLayout>
</FrameLayout>
Community
  • 1
  • 1
Tima
  • 12,765
  • 23
  • 82
  • 125

1 Answers1

8

Add

compile 'com.android.support:transition:25.2.0'

to your app dependencies. Then, add this line before change gravity (TransitionManager from android.support.transition package)

TransitionManager.beginDelayedTransition(parentOfAnimatedView);

For example

mContainer.setOnClickListener(v -> {
        TransitionManager.beginDelayedTransition((ViewGroup) mContainer.getParent());
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mContainer.getLayoutParams();
        layoutParams.gravity = Gravity.TOP;
        mContainer.setLayoutParams(layoutParams);
    });
princeparadoxes
  • 498
  • 3
  • 10
  • Is there any callback for when the animation is complete? – Alexander Sagen Nov 23 '19 at 16:01
  • 1
    Try this: val transition = AutoTransition() transition.addListener(object : TransitionListenerAdapter() { override fun onTransitionEnd(transition: Transition) { // your code } }) TransitionManager.beginDelayedTransition(parentOfAnimatedView, transition) – princeparadoxes Dec 03 '19 at 12:11