1

How to change the height of the view to match_parent when there is a click?

public class ResizeAnimation extends Animation {
    final int startHeight;
    final int targetHeight;
    private final boolean isOpen;
    View view;

    public ResizeAnimation(View view, int height, boolean isOpen) {
        this.view = view;
        this.targetHeight = height;
        this.isOpen = isOpen;
        startHeight = view.getHeight();
    }

    @Override
     protected void applyTransformation(float interpolatedTime, Transformation t) {
        int newHeight;
        if (isOpen) {
            newHeight = (int) (startHeight + (targetHeight - startHeight) * interpolatedTime);
        } else {
            newHeight =  (int) (startHeight + targetHeight * interpolatedTime);
        }
        view.getLayoutParams().height = newHeight;
        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;
    }
}

ResizeAnimation resizeAnimation = new ResizeAnimation(view, MATCH_PARENT, false);
resizeAnimation.setDuration(500);
view.startAnimation(resizeAnimation);
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126

1 Answers1

1

Your animation doesn't work because you are passing View.MATCH_PARENT(which value is -1) as target height. Quoting the documentation:

int MATCH_PARENT [...] Constant Value: -1 (0xffffffff)

You have to pass the real target height. You can accomplish that measuring the future target height in the parent layout after it has been rendered (I recommend you ViewTreeObserver.onGlobalLayout() for that).

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70