title: Android: How does GridView auto_fit find the number of columns?
body: <p>I would like to understand better how <code>Gridview</code> works, in particular <code>auto_fit</code>.
  Here is the XML layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;GridView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/gridview"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:columnWidth="60dp"
      android:numColumns="auto_fit"
  /&gt;
  </code></pre>
  
  <p>And it works fine with a series of six thumbnails (48*48 pixels).  In portrait mode, it displays one row, six columns.</p>
  
  <p><img src="https://i.stack.imgur.com/BODHg.png" alt="with"></p>
  
  <p>What I don't understand is why the line <code>android:columnWidth="60dp"</code> is necessary, because <a href="http://developer.android.com/reference/android/widget/GridView.html#attr_android:numColumns" rel="noreferrer">auto_fit</a> is expected to find the right number of columns.<br>
  Without the line <code>android:columnWidth="60dp"</code>, it displays a grid 3 rows and 2 columns.</p>
  
  <p><img src="https://i.stack.imgur.com/g8Wip.png" alt="without"></p>
  
  <p>Here is the <code>ImageAdapter</code> class:</p>
  
  <pre><code>package com.examples.HelloGridView;
  
  import android.content.Context;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.ImageView;
  
  public class ImageAdapter extends BaseAdapter {
      private Context mContext;
  
      public ImageAdapter(Context c) {
          mContext = c;
      }
  
      public int getCount() {
          return mThumbIds.length;
      }
  
      public Object getItem(int position) {
          return null;
      }
  
      public long getItemId(int position) {
          return 0;
      }
  
      // create a new ImageView for each item referenced by the Adapter
      public View getView(int position, View convertView, ViewGroup parent) {
          ImageView imageView;
          if (convertView == null) {  // if it's not recycled, initialize some attributes
              imageView = new ImageView(mContext);
              imageView.setPadding(0, 0, 0, 0);
          } else {
              imageView = (ImageView) convertView;
          }
  
          imageView.setImageResource(mThumbIds[position]);
          return imageView;
      }
  
      // references to our images
      private Integer[] mThumbIds = {
              R.drawable.ic_1, R.drawable.ic_2,
              R.drawable.ic_3, R.drawable.ic_4,
              R.drawable.ic_5, R.drawable.ic_6
      };
  }
  </code></pre>
  
  <p>Thank you for your help.</p>