title: View Animations in Android Collapse/Expand Views in LinearLayout
body: <p>I added Images view dynamically to my Linear layout, </p>
  
  <p>I want to achieve </p>
  
  <p><a href="https://i.stack.imgur.com/4oFGv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4oFGv.png" alt="enter image description here"></a></p>
  
  <p>Here is sample code what I have done</p>
  
  <p><strong>MainActivity</strong> </p>
  
  <pre><code> package ksp.com.animationssample;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.animation.AccelerateInterpolator;
  import android.view.animation.Animation;
  import android.view.animation.TranslateAnimation;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  
  public class MainActivity extends AppCompatActivity {
  
      private Button btn_expand;
      private Button btn_collapse;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          btn_expand = (Button) findViewById(R.id.btn_expand);
          btn_collapse = (Button) findViewById(R.id.btn_collapse);
          LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
          final LinearLayout layout = (LinearLayout) findViewById(R.id.imageLayout);
  
  
          for (int i = 0; i &lt; 3; i++) {
              final ImageView image = new ImageView(MainActivity.this);
              image.setLayoutParams(vp);
              if (i == 0)
                  image.setImageDrawable(getResources().getDrawable(R.drawable.item_image1));
              else image.setImageDrawable(getResources().getDrawable(R.drawable.item_image2));
              if (i == 2)
                  image.setVisibility(View.VISIBLE);
              else
                  image.setVisibility(View.GONE);
              layout.addView(image);
          }
  
  
          btn_expand.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  expandOrCollapse(layout.getChildAt(2), true, layout.getChildAt(0).getHeight() + layout.getChildAt(1).getHeight());
                  expandOrCollapse(layout.getChildAt(1), true, layout.getChildAt(0).getHeight());
                  expandOrCollapse(layout.getChildAt(0), true, 0);
  
  
              }
          });
          btn_collapse.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  expandOrCollapse(layout.getChildAt(0), false, 0);
                  expandOrCollapse(layout.getChildAt(1), false, layout.getChildAt(0).getHeight());
                  expandOrCollapse(layout.getChildAt(2), false, layout.getChildAt(0).getHeight() + layout.getChildAt(1).getHeight());
              }
          });
  
  
      }
  
  
      public void expandOrCollapse(final View v, boolean is_expand, final int animheight) {
          TranslateAnimation anim = null;
          if (is_expand) {
              anim = new TranslateAnimation(0.0f, 0.0f, -animheight, 0.0f);
              v.setVisibility(View.VISIBLE);
              Animation.AnimationListener expandedlistener = new Animation.AnimationListener() {
                  @Override
                  public void onAnimationStart(Animation animation) {
                  }
  
                  @Override
                  public void onAnimationRepeat(Animation animation) {
                  }
  
                  @Override
                  public void onAnimationEnd(Animation animation) {
  
  
                  }
              };
  
              anim.setAnimationListener(expandedlistener);
          } else {
              anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, -animheight);
              Animation.AnimationListener collapselistener = new Animation.AnimationListener() {
                  @Override
                  public void onAnimationStart(Animation animation) {
                  }
  
                  @Override
                  public void onAnimationRepeat(Animation animation) {
                  }
  
                  @Override
                  public void onAnimationEnd(Animation animation) {
                      v.setVisibility(View.GONE);
                  }
              };
  
              anim.setAnimationListener(collapselistener);
          }
  
          anim.setDuration(1500);
          anim.setInterpolator(new AccelerateInterpolator(0.5f));
          v.startAnimation(anim);
      }
  }
  </code></pre>
  
  <p><strong>activity_mainn.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:orientation="vertical"
      android:background="@android:color/holo_blue_dark"
      tools:context="ksp.com.animationssample.MainActivity"&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:id="@+id/btn_expand"
          android:text="Expand"
          /&gt;
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/btn_collapse"
          android:text="Collopse"/&gt;
  &lt;ScrollView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:scrollbars="vertical"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:id="@+id/imageLayout"
          android:gravity="center"
  
          android:orientation="vertical"
          android:layout_height="wrap_content"&gt;
      &lt;/LinearLayout&gt;
      &lt;!--android:animateLayoutChanges="true"--&gt;
  &lt;/ScrollView&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>when I click <strong><em>expand</em></strong> button at the first time is not showing the animation from the second time it's working. </p>
  
  <p>enable Visible the item after click on <strong><em>collapse</em></strong> button.</p>
  
  <p><strong>What to do next</strong> :
  When I selected any View item  it has to show select animation then collapse animation, after collapse it has to appear as top View like I mention in Image above. Currently I am hard code the count of the views and wrote static animations for each view with static heights of animations i.e (<code>expandOrCollapse(view, height_of_view)</code>)</p>
  
  <p>I search a while for this, but no luck.</p>
  
  <p>I follow the <a href="https://stackoverflow.com/a/13381228/5955593">Vie expand/collapse</a> and <a href="https://stackoverflow.com/a/23932782/5955593">smooth expand / collapse </a> but they can't help me expand all views in Linear layout. </p>
  
  <p>Can we do this List views are recycler view or something ? </p>
  
  <p>for time saving I have added my sample to git hub you can try it <a href="https://github.com/kathisai/AnimationsSample" rel="nofollow noreferrer">Animation Sample Github</a></p>
  
  <p>Suggest me, please. </p>