title: Android expand/collapse RelativeLayout
body: <p>I've tried various solutions from this topic (see. <a href="https://stackoverflow.com/questions/4946295/android-expand-collapse-animation/40794881#40794881">Android: Expand/collapse animation</a> ) and none of them really worked for me. I'm sure that the most voted ones are good and the reason that it won't work is that I don't understand something. Please tell me what i'm doing wrong. Thank you in advance :)</p>
  
  <p><strong>EDIT:</strong> The main point of my problem is, that when I click on the element 'relativeZaplon' it expands very well, yet when I want to collapse it, it does not correspond.</p>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code> public class MainActivity extends AppCompatActivity {
  
      private boolean isVisible = false;
      private RelativeLayout mRelativeZaplon;
      private RelativeLayout mRelativeToSlide;
      private ExpandOrCollapse mAnimationManager;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          mAnimationManager = new ExpandOrCollapse();
          mRelativeZaplon = (RelativeLayout) findViewById(R.id.relativeZaplon);
          mRelativeToSlide = (RelativeLayout) findViewById(R.id.relativevToSlide);
          mRelativeZaplon.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (isVisible) {
                      mAnimationManager.collapse(mRelativeToSlide, 1000, 200);
                      isVisible = false;
                  } else if (!isVisible){
                      mAnimationManager.expand(mRelativeToSlide, 1000, 200);
                      isVisible = true;
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>Expand/Collapse Class</strong> </p>
  
  <pre><code>    public class ExpandOrCollapse {
  
      public static void expand(final View v, int duration, int targetHeight) {
          int prevHeight  = v.getHeight();
          v.setVisibility(View.VISIBLE);
          ValueAnimator valueAnimator = ValueAnimator.ofInt(prevHeight, targetHeight);
          valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
              @Override
              public void onAnimationUpdate(ValueAnimator animation) {
                  v.getLayoutParams().height = (int) animation.getAnimatedValue();
                  v.requestLayout();
              }
          });
          valueAnimator.setInterpolator(new DecelerateInterpolator());
          valueAnimator.setDuration(duration);
          valueAnimator.start();
      }
  
      public static void collapse(final View v, int duration, int targetHeight) {
          int prevHeight  = v.getHeight();
          ValueAnimator valueAnimator = ValueAnimator.ofInt(prevHeight, targetHeight);
          valueAnimator.setInterpolator(new DecelerateInterpolator());
          valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
              @Override
              public void onAnimationUpdate(ValueAnimator animation) {
                  v.getLayoutParams().height = (int) animation.getAnimatedValue();
                  v.requestLayout();
              }
          });
          valueAnimator.setInterpolator(new DecelerateInterpolator());
          valueAnimator.setDuration(duration);
          valueAnimator.start();
      }
  }
  </code></pre>
  
  <p><strong>XML</strong></p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;RelativeLayout
          android:background="@color/colorPrimaryDark"
          android:id="@+id/relativevToSlide"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:visibility="gone"&gt;
      &lt;/RelativeLayout&gt;
      &lt;RelativeLayout
          android:id="@+id/relativeZaplon"
          android:background="@color/colorAccent"
          android:layout_width="match_parent"
          android:layout_height="20dp"
          android:layout_below="@+id/relativevToSlide"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true"&gt;
      &lt;/RelativeLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>