Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a complete solution with an extended ArrayAdapter that allows icons.</p> <p>See design notes for dialogs at <a href="http://developer.android.com/design/building-blocks/dialogs.html" rel="noreferrer">http://developer.android.com/design/building-blocks/dialogs.html</a> Iconogaphy at <a href="http://developer.android.com/design/style/iconography.html" rel="noreferrer">http://developer.android.com/design/style/iconography.html</a> and IconPacks at <a href="http://developer.android.com/design/downloads/index.html" rel="noreferrer">http://developer.android.com/design/downloads/index.html</a></p> <p><strong>Note</strong> that the size for these looks pretty good at 48 x 48 dp, which isn't a bundled size, so you'll have to scale your own icon from the downloads.</p> <p><strong>USAGE</strong>: </p> <pre><code> @Override public void onClick(View v) { final String [] items = new String[] {"From Gallery", "From Camera"}; final Integer[] icons = new Integer[] {R.drawable.dialog_gallery_icon, R.drawable.dialog_camera_icon}; ListAdapter adapter = new ArrayAdapterWithIcon(getActivity(), items, icons); new AlertDialog.Builder(getActivity()).setTitle("Select Image") .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item ) { Toast.makeText(getActivity(), "Item Selected: " + item, Toast.LENGTH_SHORT).show(); } }).show(); } </code></pre> <p><strong>ArrayAdapterWithIcon.java</strong></p> <pre><code>public class ArrayAdapterWithIcon extends ArrayAdapter&lt;String&gt; { private List&lt;Integer&gt; images; public ArrayAdapterWithIcon(Context context, List&lt;String&gt; items, List&lt;Integer&gt; images) { super(context, android.R.layout.select_dialog_item, items); this.images = images; } public ArrayAdapterWithIcon(Context context, String[] items, Integer[] images) { super(context, android.R.layout.select_dialog_item, items); this.images = Arrays.asList(images); } public ArrayAdapterWithIcon(Context context, int items, int images) { super(context, android.R.layout.select_dialog_item, context.getResources().getTextArray(items)); final TypedArray imgs = context.getResources().obtainTypedArray(images); this.images = new ArrayList&lt;Integer&gt;() {{ for (int i = 0; i &lt; imgs.length(); i++) {add(imgs.getResourceId(i, -1));} }}; // recycle the array imgs.recycle(); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); TextView textView = (TextView) view.findViewById(android.R.id.text1); if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN_MR1) { textView.setCompoundDrawablesRelativeWithIntrinsicBounds(images.get(position), 0, 0, 0); } else { textView.setCompoundDrawablesWithIntrinsicBounds(images.get(position), 0, 0, 0); } textView.setCompoundDrawablePadding( (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics())); return view; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload