piojo piojo - 1 month ago 10
Android Question

How to expand a layout height with animation?

I couldn't find a good example for how to do this.

I have a RelativeLayout set with x height.

I want to add a button which expands the height to x+y height.

can someone refer me to a good example on how to do it programmatically?

Answer

Please check below new edited answer as below. But here you need to know the exact new height.

public class LayoutAnimationActivity extends Activity 
{
    RelativeLayout ril1;
    Button btn;
    int initialHeight;
    int actualHeight;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        ril1=(RelativeLayout)findViewById(R.id.relativeLayout1);
        btn=new Button(this);
        btn.setWidth(100);
        btn.setHeight(200);
        btn.setText("Button");
        actualHeight=210;
        Ani a=new Ani();
        a.setDuration(2000);
        ril1.startAnimation(a);
    }

    class Ani extends Animation
    {
        public Ani() {
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            int newHeight;

            newHeight = (int)(initialHeight * interpolatedTime);

            ril1.removeAllViews();
            btn.setWidth(100);
            btn.setHeight(300);
            btn.setText("as");
            ril1.addView(btn);             
            ril1.getLayoutParams().height = newHeight;
            ril1.requestLayout();
        }

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

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