title: Setting margin programmatically to CardView
body: <p>I have a <code>CardView</code> in my XML layout and I need to set margins to it programmatically (This is because I don't need margin at all times. Based on few conditions, I either set or remove the margin).</p>
  <p>Here's how I did it:</p>
  <pre class="lang-java prettyprint-override"><code>CardView.LayoutParams layoutParams = (CardView.LayoutParams) myCardView.getLayoutParams();
  layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
  myCardView.setLayoutParams(layoutParams);
  </code></pre>
  <p>This worked fine. It put <code>2dp</code> margin at the bottom of my <code>CardView</code>. However, I get this in my logcat:</p>
  <pre class="lang-java prettyprint-override"><code>java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
  </code></pre>
  <p>So I changed <code>CardView.LayoutParams</code> to <code>FrameLayout.LayoutParams</code>, like this:</p>
  <pre class="lang-java prettyprint-override"><code>FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) myCardView.getLayoutParams();
  layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
  myCardView.setLayoutParams(layoutParams);
  </code></pre>
  <p>And yet again, it works but I get the same error as above.</p>
  <p>So I modified it one more time to use <code>LinearLayout.LayoutParams</code>.</p>
  <pre class="lang-java prettyprint-override"><code>LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) myCardView.getLayoutParams();
  layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
  myCardView.setLayoutParams(layoutParams);
  </code></pre>
  <p>When I do this, I do NOT get the error however <strong>it does not set any margin</strong>.</p>
  <p>Should I just ignore the error? It just doesn't seem right.</p>
  <p><strong>EDIT:</strong></p>
  <p>Here's my <code>CardView</code> in XML:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:orientation=&quot;horizontal&quot;
      android:background=&quot;@android:color/white&quot;
      android:minHeight=&quot;@dimen/item_min_height&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;&gt;
  
      &lt;android.support.v7.widget.CardView
          android:id=&quot;@+id/cardViewAppointmentWeek&quot;
          app:cardElevation=&quot;2dp&quot;
          android:layout_marginBottom=&quot;2dp&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;&gt;
  
          &lt;LinearLayout
              android:orientation=&quot;horizontal&quot;
              android:background=&quot;?android:attr/selectableItemBackground&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;&gt;
  
              ...
  
          &lt;/LinearLayout&gt;
  
      &lt;/android.support.v7.widget.CardView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>