Animation in changing LayoutParams in LinearLayout

22,427

Solution 1

If you want to implement the slider animation for your layout then see this demo.

UPDATED

Also see http://www.inter-fuser.com/2009/07/android-transistions-slide-in-and-slide.html

Hope it will help you.

If not then let me know.

Thanks. Enjoy. :)

Solution 2

I think you just want to animate a view from 0 height to its final height, you can do this with a custom Animation:

public class ShowAnim extends Animation {
    int targetHeight;
    View view;

    public ShowAnim(View view, int targetHeight) {
        this.view = view;
        this.targetHeight = targetHeight;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) (targetHeight * interpolatedTime);
        view.requestLayout();
    }

    @Override
    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

And do this in your code to start the animation:

Animation ani = new ShowAnim(headerView, 100/* target layout height */);
ani.setDuration(2000/* animation time */);
headerView.startAnimation(ani);

Solution 3

use animateLayoutChanges xml and enableTransitionType in java or kotlin

1. add animateLayoutChanges to root layout xml

    <LinearLayout
        android:id="@+id/mainLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:orientation="vertical">

        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="0dp" />
    </LinearLayout>

2. in java

LayoutTransition layoutTransition = mainLinearLayout.layoutTransition;
layoutTransition.setDuration(5000); // Change duration
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);

editText.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; // you can set number, example: 300

editText.requestLayout();

3.in kotlin

 val layoutTransition = mainLinearLayout.layoutTransition
 layoutTransition.setDuration(5000) // Change duration
 layoutTransition.enableTransitionType(LayoutTransition.CHANGING)

 editText.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT // you can set number, example: 300 

 editText.requestLayout()

Solution 4

Since we have the layout transitions in android since JELLYBEAN we can use that instead of using the animation object.

The article below explains it in detail. https://proandroiddev.com/the-little-secret-of-android-animatelayoutchanges-e4caab2fddec

In short we would only need this code -

tView.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
tView.setLayoutParams(lp);

Here lp would be the layout params

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
                            (RelativeLayout.LayoutParams.MATCH_PARENT, newHeight);

One more thing to add would be to add this line in the layout file, to the layout that would be doing the transition.

android:animateLayoutChanges="true"
Share:
22,427
Chrishan
Author by

Chrishan

Experienced Android Developer. Please find my profile at LinkedIn

Updated on January 23, 2020

Comments

  • Chrishan
    Chrishan over 4 years

    In my app there is a LinearLayout which has 0 Layout height. When I click the button this layout height should be LayoutParams.WRAP_CONTENT. This is the code I use in onclicklistner.

    LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    slider.setLayoutParams(lp);
    

    I want to animate this. How can I set animation to slider.