You will basically need to create your own ListAdapter
subclassing one of the available adapter classes and dialog (using builder.setAdapter(...)
). If you have an array or a list of objects / objects, subclasses of the ArrayAdapter
, you probably want to explore.
In the Adapter subclass, you override the getView(...)
method (among others) and fill out the views of your custom layout with data for the position in the list. More specifically, you will want to set an image with ImageView
and text on a TextView
.
Create class `MySimpleArrayAdapter` extending from `ArrayAdapter`.
Using the ListView.setOnItemClickListener()
method, we get the selected CheckTextView
value
#dialog.xml <?xml version="1.0" encoding="UTF-8"?> <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" > <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" /> </LinearLayout>
To create a custom dialog box
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<?> arg0, View arg1, int arg2, long arg3) {
source share