title: Changing number of columns in RecyclerView gridlayout
body: <p>I am trying to change the number of columns that appear in the recycler view (grid layout) based on the display size. However I couldn't figure out a proper way of achieving it. At the moment I am using <code>treeViewObserver</code> to change the number of columns based the change in screen size (during orientation). So if the app opens in portrait mode, number of columns (on the phone) it decides to be one, which look good but this method doesn't work when the app directly opens in landscape mode where a single streched out card in the grid is displayed on the screen.</p>
  
  <p><strong>Here recList is RecyclerView &amp; glm is GridLayoutManager used in RecyclerView</strong></p>
  
  <pre><code>    viewWidth = recList.getMeasuredWidth();
  
      cardViewWidthZZ = recList.getChildAt(0).getMeasuredWidth();
  
      if (oldWidth == 0) {
          oldWidth = cardViewWidthZZ;
      }
  
      if (oldWidth &lt;= 0)
          return;
  
      int newSpanCount = (int) Math.floor(viewWidth / (oldWidth / 1.3f));
      if (newSpanCount &lt;= 0)
          newSpanCount = 1;
      glm.setSpanCount(newSpanCount);
      glm.requestLayout();
  </code></pre>
  
  <p>Best Regards</p>