Skip to content

Instantly share code, notes, and snippets.

@Budincsevity
Last active January 14, 2022 09:00
Show Gist options
  • Save Budincsevity/5c7873f50a3972c8cbc8 to your computer and use it in GitHub Desktop.
Save Budincsevity/5c7873f50a3972c8cbc8 to your computer and use it in GitHub Desktop.
Change the height of a layout with ValueAnimator
private RelativeLayout mCalendarView;
.
.
.
private void expandLayout() {
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
mCalendarView.measure(widthSpec, heightSpec);
ValueAnimator animator = slideAnimator(0, 750);
nimator.start();
}
private void collapseLayout() {
int finalHeight = mCalendarView.getHeight();
ValueAnimator animator = slideAnimator(finalHeight, 0);
animator.start();
}
private ValueAnimator slideAnimator(int start, int end) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = mCalendarView.getLayoutParams();
layoutParams.height = value;
mCalendarView.setLayoutParams(layoutParams);
}
});
return animator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment