72

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?

g.revolution
  • 11,962
  • 23
  • 81
  • 107

8 Answers8

153
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(); 
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
18

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.

Westy92
  • 19,087
  • 4
  • 72
  • 54
A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38
  • Don't forget the closing brackets on that AccelerateDecelerateInterpolator instance creation – RTF Mar 25 '16 at 13:56
  • 20
    `scale` will grow the `view` in both directions, and stretch its child views _(if it has any)_. The __OP__ wants to animate the `height` of the `view` only. – Mudlabs Jun 26 '19 at 02:37
16

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.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • 1
    This is exactly what I was searching for! Sadly, doesn't work on smooth animating recycler view when adding / removing items. Throws exception: Providing a LayoutTransition into RecyclerView is not supported. Please use setItemAnimator() – Aetherna Feb 06 '21 at 00:33
  • 2
    if ```android:animateLayoutChanges="true"``` didn't work in nested views, then you need to add ```linearlayout.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)``` to the parent layout which will be resized. – Amr Jul 28 '21 at 06:18
7

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()
}
Minion
  • 964
  • 14
  • 16
2
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

hmac
  • 267
  • 3
  • 9
1

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

Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29
0

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

Osama Remlawi
  • 2,356
  • 20
  • 21
-2

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

Amit Garg
  • 541
  • 5
  • 12