Android property animation: how to increase view height?

JavaAndroidAndroid AnimationObjectanimatorViewpropertyanimator

Java Problem Overview


How to increase the view height using Property Animations in Android?

ObjectAnimator a = ObjectAnimator.ofFloat(viewToIncreaseHeight, "translationY", -100);
a.setInterpolator(new AccelerateDecelerateInterpolator());
a.setDuration(1000);
a.start();

The translationY actually moves the view not increase the height. How can I increase the height of the view?

Java Solutions


Solution 1 - Java

ValueAnimator anim = ValueAnimator.ofInt(viewToIncreaseHeight.getMeasuredHeight(), -100);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        int val = (Integer) valueAnimator.getAnimatedValue();
        ViewGroup.LayoutParams layoutParams = viewToIncreaseHeight.getLayoutParams();
        layoutParams.height = val;
        viewToIncreaseHeight.setLayoutParams(layoutParams);
    }
});
anim.setDuration(DURATION);
anim.start(); 

Solution 2 - Java

You can use ViewPropertyAnimator, which can save you some lines of code :

yourView.animate()
   .scaleY(-100f)
   .setInterpolator(new AccelerateDecelerateInterpolator())
   .setDuration(1000);

that should be all you need, be sure to check the documentation and all available methods for ViewPropertyAnimator.

Solution 3 - Java

This is not a direct answer to this question. However, it may help someone.

Sometimes, we want to increase/decrease view's height because some of its child is is being added/removed (or just becoming visible/gone).

On those cases, you really can use Android default animation. As mentioned in other answers, you can set via:

<LinearLayout
    ...
    android:animateLayoutChanges="true"
.../>

Or in Java:

linearLayout.setLayoutTransition(new LayoutTransition());

If you created a custom view:

public StickerPickerView(final Context context, final AttributeSet attrs,
        final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setLayoutTransition(new LayoutTransition());
}

For me, it was pretty satisfactory but I just tested on latest APIs. That will automatically add a fade in/out when your view becomes visible. It will also animate the height/width of your view when same changes (like when you change the visibility of one of the child views etc).

So, I recommend to at least give a try.

Solution 4 - Java

Use this method in kotlin:

private fun increaseViewSize(view: View) {
    val valueAnimator = ValueAnimator.ofInt(view.measuredHeight, view.measuredHeight+20)
    valueAnimator.duration = 500L
    valueAnimator.addUpdateListener {
        val animatedValue = valueAnimator.animatedValue as Int
        val layoutParams = view.layoutParams
        layoutParams.height = animatedValue
        view.layoutParams = layoutParams
    }
    valueAnimator.start()
}

Solution 5 - Java

For kotlin you can use this method

private fun increaseViewSize(view: View, increaseValue: Int) {
    val valueAnimator =
        ValueAnimator.ofInt(view.measuredHeight, view.measuredHeight + increaseValue)
    valueAnimator.duration = 500L
    valueAnimator.addUpdateListener {
        val animatedValue = valueAnimator.animatedValue as Int
        val layoutParams = view.layoutParams
        layoutParams.height = animatedValue
        view.layoutParams = layoutParams
    }
    valueAnimator.start()
}

It will increase view's size as defined in parameter

based on @Minion answer

Solution 6 - Java

Here is my code to animate scrolling an item to center of the screen inside a recycleView

// Scrolling with animation
val valueAnimator = ValueAnimator.ofInt(getWidth() / 2)
valueAnimator.duration = 250L
valueAnimator.addUpdateListener {
    val animatedValue = valueAnimator.animatedValue as Int
    (layoutManager as LinearLayoutManager).scrollToPositionWithOffset(itemToScroll, animatedValue)
}
valueAnimator.start()
// End of scrolling with animation

Note it is a bit similar to UIView.animate in Swift, but with more complication

Solution 7 - Java

fun View.animateHeightFromTo(initialHeight: Int, finalHeight: Int) {
    val animator = ValueAnimator.ofInt(initialHeight, finalHeight)
    animator.duration = 250
    animator.addUpdateListener {
        val value = it.animatedValue as Int
        val lp = this.layoutParams
        lp.height = value
        this.layoutParams = lp
        isVisible = value != 0
    }
    animator.start()
}

The visibility setting was convenient for me but you may not want that

Solution 8 - Java

It's pretty straight forward as you can see below.

<LinearLayout android:id="@+id/container"
android:animateLayoutChanges="true"
.../>

More info below:

https://developer.android.com/training/animation/layout

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiong.revolutionView Question on Stackoverflow
Solution 1 - JavaRajan KaliView Answer on Stackoverflow
Solution 2 - JavaA Honey BustardView Answer on Stackoverflow
Solution 3 - JavaW0rmH0leView Answer on Stackoverflow
Solution 4 - JavaMinionView Answer on Stackoverflow
Solution 5 - JavaRahul GaurView Answer on Stackoverflow
Solution 6 - JavaOsama RemlawiView Answer on Stackoverflow
Solution 7 - JavahmacView Answer on Stackoverflow
Solution 8 - JavaAmit GargView Answer on Stackoverflow