title: Set Margin on RecyclerView programmatically
body: <p>I need to set the top margin on a RecyclerView programmatically, but I get this exception: </p>
  
  <p><code>java.lang.RuntimeException: Unable to resume activity java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams</code></p>
  
  <p>Here is my code:</p>
  
  <pre><code>RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)recyclerView.getLayoutParams();
  int marginTopDp = (int)getResources().getDimension(R.dimen.margin);
  int marginTopPx = (int) (marginTopDp * getResources().getDisplayMetrics().density + 0.5f);
  layoutParams.setMargins(0, marginTopPx, 0, 0);
  recyclerView.setLayoutParams(layoutParams);
  </code></pre>
  
  <p>If I use the <code>ViewGroup.LayoutParams layoutParams = recyclerView.getLayoutParams</code> as the stack trace suggests, I cannot call <code>setMargin</code> anymore as that method does not exist for <code>ViewGroup.LayoutParams</code>.</p>
  
  <p>Any help would be appreciated.</p>