Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You basically will have to create your own <code>ListAdapter</code> by subclassing one of the available Adapter classes and supply that to the dialog (using <code>builder.setAdapter(...)</code>). If you have an array or list of items/objects, subclassing <code>ArrayAdapter</code> is probably what you'll want to look into.</p> <p>In your Adapter subclass, you override the <code>getView(...)</code> method (amongst others) and fill the views of your custom layout with data for the supplied position in the list. More specifically, you'll want to set an image against an <code>ImageView</code> and text to a <code>TextView</code>.</p> <blockquote> <pre><code> Create class `MySimpleArrayAdapter` extending from `ArrayAdapter`. </code></pre> </blockquote> <p>Using <code>ListView.setOnItemClickListener()</code> method we get selected value of <code>CheckTextView</code></p> <blockquote> <pre><code>#dialog.xml &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/whitebox_bg" android:orientation="horizontal" &gt; &lt;CheckedTextView android:id="@+id/textDialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checkMark="?android:attr/listChoiceIndicatorSingle" android:gravity="center_vertical" android:maxLines="10" android:singleLine="false" android:text="" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#000000" /&gt; &lt;/LinearLayout&gt; </code></pre> </blockquote> <p>To create a custom dialog box </p> <blockquote> <pre><code> String [] view_location = {"Red", "Green", "Blue"}; TextView label = (TextView) findViewById(R.id.selected_dept); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Select Location"); ListView listview = new ListView(this); listview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); listview.setCacheColorHint(0); listview.setBackgroundColor(Color.WHITE); if (view_location != null) { MySimpleArrayAdapter choice_arrayAdapter = new MySimpleArrayAdapter(this, view_location,label.getText().toString()); listview.setAdapter(choice_arrayAdapter); builder.setView(listview); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub TextView label = (TextView) findViewById(R.id.selected_dept); label.setText(view_location[arg2]); survedObjectId = view_location_id[arg2]; dInterface.dismiss(); } }); } Dialog dialog = builder.create(); dInterface = dialog; dialog.getWindow().setLayout(200, 400); dialog.show();' // `Extends From ArrayAdapter 'public class MySimpleArrayAdapter extends ArrayAdapter&lt;String&gt; { private final Context context; private final String[] values; private final String selectedText; public MySimpleArrayAdapter(Context context, String[] values, String selectedText) { super(context, R.layout.dialog_text, values); this.context = context; this.values = values; this.selectedText = selectedText; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.dialog_text, parent, false); CheckedTextView textView = (CheckedTextView) rowView.findViewById(R.id.textDialog); textView.setText(values[position]); if(textView.getText().toString().equals(selectedText)) { textView.setChecked(true); } else { textView.setChecked(false); } return rowView; } }' </code></pre> </blockquote>
 

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