Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe the simplest approach is to extend <code>Animation</code> class and override <code>applyTransformation()</code> to change the view's height as follows:</p> <pre><code>import android.view.View; import android.view.ViewGroup.LayoutParams; import android.view.animation.Animation; import android.view.animation.Transformation; import android.widget.LinearLayout; public class MyCustomAnimation extends Animation { public final static int COLLAPSE = 1; public final static int EXPAND = 0; private View mView; private int mEndHeight; private int mType; private LinearLayout.LayoutParams mLayoutParams; public MyCustomAnimation(View view, int duration, int type) { setDuration(duration); mView = view; mEndHeight = mView.getHeight(); mLayoutParams = ((LinearLayout.LayoutParams) view.getLayoutParams()); mType = type; if(mType == EXPAND) { mLayoutParams.height = 0; } else { mLayoutParams.height = LayoutParams.WRAP_CONTENT; } view.setVisibility(View.VISIBLE); } public int getHeight(){ return mView.getHeight(); } public void setHeight(int height){ mEndHeight = height; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); if (interpolatedTime &lt; 1.0f) { if(mType == EXPAND) { mLayoutParams.height = (int)(mEndHeight * interpolatedTime); } else { mLayoutParams.height = (int) (mEndHeight * (1 - interpolatedTime)); } mView.requestLayout(); } else { if(mType == EXPAND) { mLayoutParams.height = LayoutParams.WRAP_CONTENT; mView.requestLayout(); }else{ mView.setVisibility(View.GONE); } } } } </code></pre> <p>To use it, set your <code>onclick()</code> as follows:</p> <pre><code>int height; @Override public void onClick(View v) { if(view2.getVisibility() == View.VISIBLE){ MyCustomAnimation a = new MyCustomAnimation(view2, 1000, MyCustomAnimation.COLLAPSE); height = a.getHeight(); view2.startAnimation(a); }else{ MyCustomAnimation a = new MyCustomAnimation(view2, 1000, MyCustomAnimation.EXPAND); a.setHeight(height); view2.startAnimation(a); } } </code></pre> <p>Regards.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload