title: Dialog is too wide when it contains a ListView
body: <p>I'm having a problem with the width of a dialog when it contains a <code>ListView</code>: the dialog always nearly fills the width of the screen. Here's an example of the problem:</p>
  
  <p><img src="https://i.stack.imgur.com/WyAqx.png" alt="overly wide dialog"></p>
  
  <p>I need the dialog width to match the icons. I've tried fixing this by playing with dialog styles as described <a href="https://stackoverflow.com/questions/13982321/android-listview-array-adapter-not-wrapping-content-correctly">here</a> and <a href="https://stackoverflow.com/questions/6173358/dialog-with-custom-view-setcanceledontouchoutside-doesnt-work/8876358#8876358">here</a>, to no avail. I think those threads are on the wrong track (at least for this problem), because (1) they don't solve the problem and (2) I can get the dialog to size as desired by simply not using a <code>ListView</code>. For instance, if I replace the <code>ListView</code> with a simple <code>TextView</code> (details below), everything sizes as desired:</p>
  
  <p><img src="https://i.stack.imgur.com/udgzg.png" alt="compact dialog"></p>
  
  <p>Here's the code I'm using to display the dialog:</p>
  
  <pre><code>private void showDisplayStyleDialog(int id, int choices, int values) {
      final Dialog dlg = new Dialog(this);
      dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
      dlg.setContentView(R.layout.display_style_dialog);
      final ListView lv = (ListView) dlg.findViewById(android.R.id.list);
      if (lv != null) {
          displayStyleAdapter.setChoices(choices, values);
          lv.setAdapter(displayStyleAdapter);
          lv.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long itemId) {
                  dlg.dismiss();
                  setDisplayStyle(DisplayStyle
                          .valueOf(displayStyleAdapter.mValues[position]));
              }
  
          });
      }
      dlg.setCanceledOnTouchOutside(true);
      dlg.show();
  }
  </code></pre>
  
  <p>I also tried creating the dialog using an <code>AlertDialog.Builder</code>, with no improvement. The dialog layout (<code>display_style_dialog.xml</code>) is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" &gt;
  
      &lt;ListView
          android:id="@android:id/list"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@android:color/white" &gt;
      &lt;/ListView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The adapter loads every row from this layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/holder"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:orientation="vertical"
      android:padding="5dp" &gt;
  
      &lt;ImageView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/image"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/display_style_image_bg"
          android:contentDescription="@string/cd_display_style" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Examining the view hierarchy with the Android Debug Monitor reveals that only the <code>ListView</code> itself is the full width of the dialog; each row is (as desired) sized to match the icon width. It's the <code>ListView</code> itself that is forcing the wide dialog. If I change <code>display_style_dialog.xml</code> to:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" &gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/test_string"
          android:textColor="@android:color/black"
          android:background="@android:color/white" &gt;
      &lt;/TextView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>and make <em>no other changes</em> to code or resources, the result is the compact dialog shown above. As far as I can tell, the problem is strictly due to the presence of the <code>ListView</code> in the dialog.</p>
  
  <p>I think I can work around this in the short term by placing a number of <code>ImageView</code>s in a <code>VerticalLayout</code> inside a <code>ScrollView</code>. Unfortunately, the arrangement and number of icons varies considerably according to the internal state of the app, so this is not a sustainable approach; in the long run, I really need to use a <code>ListView</code>. Any ideas on how to resolve this issue?</p>