You can try to use setView
method in AlertDialog.Builder
.
1.Add a alerdialog_layout
as View .
2.Use ListView
in it .
3.Use setView
to add data to it .
Note
If you want to use dynamic icons , you can use BaseAdapter
.
alerdialog_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
item_dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round"/>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="text1"/>
</LinearLayout>
Java code
/**
* add dialog
*/
protected void addDailog() {
CharSequence options[] = new CharSequence[]{"RETEN", "ACCIDENTE VIAL", "INUNDACION", "ABUSO", "ASALTO / VIOLENCIA"};
View view = LayoutInflater.from(this).inflate(R.layout.alerdialog_layout, null);
ListView listView = view.findViewById(R.id.list_view);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.item_dialog, R.id.tv1, options){};
listView.setAdapter(arrayAdapter);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("AlertDialog")
.setMessage("AlertDialog Message")
.setView(view)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("No", null)
.create();
dialog.show();
}
OUTPUT
