title: Removing/Adding constraint programmatically in ConstraintLayout
body: <p>I want to remove and add constraint programmatically based on some condition. Here are the screenshots:</p>
  
  <p><a href="https://i.stack.imgur.com/IfUS9.png" rel="noreferrer"><img src="https://i.stack.imgur.com/IfUS9.png" alt="The button &quot;create ad&quot; has a top constraint"></a></p>
  
  <p>and I want to remove it like this but in code:</p>
  
  <p><a href="https://i.stack.imgur.com/q21qC.png" rel="noreferrer"><img src="https://i.stack.imgur.com/q21qC.png" alt="here is button with removed constraint on top"></a></p>
  
  <p>so the same effect in want to achieve programmatically</p>
  
  <p>and here is the code that I tried:</p>
  
  <pre><code>    if (advertisements.size() &gt; 0) { //my own condition
          ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();
          layoutParams.topToBottom = R.id.imvEmpty; //the imageview that is in center of the view
          btnCreateAd.setLayoutParams(layoutParams);
          recyclerView.setVisibility(View.VISIBLE);
          txvMyAdEmptyText.setVisibility(View.GONE);
          imvEmpty.setVisibility(View.GONE);
          adapter.setList(advertisements);
          adapter.notifyDataSetChanged();
      } else {
          ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();
          layoutParams.topToBottom = -1; //here i am trying to remove top constraint but it doesn't work
          btnCreateAd.setLayoutParams(layoutParams);
  
          recyclerView.setVisibility(View.GONE);
          txvMyAdEmptyText.setVisibility(View.VISIBLE);
          imvEmpty.setVisibility(View.VISIBLE);
          adapter.setList(new ArrayList&lt;Advertisement&gt;());
      }
      mConstraintView.invalidate(); //this is my constraint view
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have <a href="https://developer.android.com/reference/android/support/constraint/ConstraintSet.html" rel="noreferrer">tried using</a> <code>ConstraintSet</code> also, but the result was even different somehow my RecyclerView (which is set to boundaries of parent view) was disappearing</p>
  
  <pre><code> ConstraintSet set = new ConstraintSet();
      set.clone(parentView);
  
      if (advertisements.size() &gt; 0) {
  
          recyclerView.setVisibility(View.VISIBLE);
          txvMyAdEmptyText.setVisibility(View.GONE);
          imvEmpty.setVisibility(View.GONE);
          adapter.setList(advertisements);
          adapter.notifyDataSetChanged();
  
      } else {
  
          set.connect(btnCreateAd.getId(), ConstraintSet.TOP, imvEmpty.getId(), ConstraintSet.BOTTOM, 0);
  
          recyclerView.setVisibility(View.GONE);
          txvMyAdEmptyText.setVisibility(View.VISIBLE);
          imvEmpty.setVisibility(View.VISIBLE);
          adapter.setList(new ArrayList&lt;Advertisement&gt;());
      }
      set.connect(btnCreateAd.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
      set.connect(btnCreateAd.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0);
      set.connect(btnCreateAd.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
  
      set.applyTo(parentView);
  </code></pre>