1

I want an AlertDialog with text and icons. I have created two arrays, one for the dialog items and one for the dialog items icons, but it does't work. Here is my code so far:

CharSequence options[] = new CharSequence[] {"RETEN", "ACCIDENTE VIAL", "INUNDACION", "ABUSO", "ASALTO / VIOLENCIA"};
builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_DeviceDefault_Settings);
builder.setCancelable(false);
Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.alertas);
builder.setIcon(icons);

builder.setTitle(getContext().getString(R.string.select_alert_type));
builder.setItems(options, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // the user clicked on options[which]

        //1.RECUPERAR OPCION SELECCIONADA

        if (which == 0){ ... }

        ...

    });

I get a warning at line :

builder.setIcon(icons);
Brian
  • 7,955
  • 16
  • 66
  • 107
mvasco
  • 4,965
  • 7
  • 59
  • 120
  • Why do you `setIcon(TypeArray)`? From https://developer.android.com/reference/android/app/AlertDialog.Builder.html, I only see `int id` and `Drawable` are here. – CoXier Dec 21 '17 at 01:17
  • 1
    `builder.setIcon` is not the method you're looking for. That method is a legacy method for `AlertDialog` when Android used to have an icon in the title section of dialog interfaces – they don't do that anymore. If you want a list of options with icons, you'll have to implement a custom dialog UI. Try looking at this: https://stackoverflow.com/a/15453996/394933. – Brian Dec 21 '17 at 01:19
  • @Brian, thank you, I will try to implement the proposed solution. – mvasco Dec 21 '17 at 01:21

1 Answers1

6

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 enter image description here

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42