title: How to set staggered grid span count to use available screen width?
body: <p>I'm using vertical StaggeredGridLayoutManager to display some thumbnails. Each row contains a cardview with 1 inch width and 150dp height. My question is How can i set the staggered grid span count to use available physical width of the screen?</p>
  
  <p>CardViewRow.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:card_view="http://schemas.android.com/apk/res-auto"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="5dp"
      android:orientation="vertical"
      card_view:cardCornerRadius="0dp"
      card_view:cardUseCompatPadding="true" &gt;
  
  
     &lt;LinearLayout
         android:gravity="center"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/card_selector"
         android:orientation="vertical"&gt;
         &lt;FrameLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"&gt;
  
  
         &lt;ImageView
             android:id="@+id/thumbnail"
             android:layout_width="300dp"
             android:layout_height="150dp"
             android:scaleType="centerCrop"/&gt;
  
             &lt;LinearLayout
                 android:weightSum="2"
                 android:padding="5dp"
                 android:layout_gravity="bottom"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:orientation="horizontal"
                 android:background="#80000000"&gt;
  
                 &lt;TextView
                     android:id="@+id/size_txt"
                     android:layout_weight="1"
                     android:textSize="12sp"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:textColor="@color/silver"
                     android:gravity="left"/&gt;
                 &lt;TextView
                     android:layout_weight="1"
                     android:gravity="right"
                     android:textColor="@color/silver"
                     android:id="@+id/time_txt"
                     android:textSize="12sp"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"/&gt;
  
             &lt;/LinearLayout&gt;
             &lt;View
                 android:id="@+id/selector"
                 android:visibility="invisible"
                 android:background="#950096ff"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"&gt;
  
             &lt;/View&gt;
         &lt;/FrameLayout&gt;
         &lt;TextView
             android:id="@+id/title_txt"
             android:padding="8dp"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:maxLines="5" /&gt;
         &lt;/LinearLayout&gt;
  
  
  &lt;/android.support.v7.widget.CardView&gt;
  </code></pre>
  
  <p>Currently I'm using this code. (Credits: mstrengis)</p>
  
  <pre><code>  DisplayMetrics metrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(metrics);
          int cardWidth =(int) metrics.xdpi; //CardWidth==1Inch.
          int spans = (int) Math.floor(mRecyclerView.getContext().getResources().getDisplayMetrics().widthPixels / (float) cardWidth);
          sglm.setSpanCount(spans);
  </code></pre>
  
  <p>/* This works perfectly on my nexus 5 and samsung tab, But on my lenovo phone the card width is less than 1.5CM and span count is more than what i expected.*/</p>