9

I have an alert dialog that does not show the icon that I set using "setIcon".
I use Android Studio and Android Emulator API 19/ Android 4.4.2.

I can run the app, the dialog is shown without icon, no error.
But Android studio marks the line that contains "setIcon" and offers me "Introduce local variable" and "Make method call chain into call sequence"

So my question : Why is the icon not shown?

My Code:

AlertDialog.Builder builder = new AlertDialog.Builder(this );  

builder

.setMessage("Repeat game?")
.setIcon(android.R.drawable.ic_dialog_alert)         //<--- icon does not show
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        //Yes button clicked, do something

    }
})
.setNegativeButton("No", null)                      //Do nothing on no
.show();
Spacewalker
  • 451
  • 1
  • 8
  • 29

6 Answers6

25

Your title is empty, so android will not display it, therefore no icon will be shown either.

Set it to empty string:

builder.setTitle("")...<all the other stuff>
Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20
7

You should use something like this:

builder.setIcon(getResources().getDrawable(android.R.drawable.ic_dialog_alert));

Try this :)

3

You just need to add title, because the position of your icon is beside your title.

.setTitle("Attention")
.setIcon(android.R.drawable.ic_dialog_alert)  
ahmfarisi
  • 154
  • 8
2

It might be that you need to set the title as well. If you do not like the new layout you might want to look at this question.

AlertDialog.Builder builder = new AlertDialog.Builder(this );  
builder
    .setMessage("Repeat game?")
    .setTitle("")
    .setIcon(android.R.drawable.ic_dialog_alert)         //<--- icon does not show
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            //Yes button clicked, do something
        }
    })
    .setNegativeButton("No", null)                      //Do nothing on no
    .show();
Community
  • 1
  • 1
mach
  • 8,315
  • 3
  • 33
  • 51
0

You can use dialogBuiler.setView(your_custom_layout)

for instance custom_dialog_layout.xml you can beautify as per your chioce

<?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="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="7dp"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/custom_diag_img_view"
            android:src="@android:drawable/ic_dialog_alert"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/custom_diag_title_tv"
            style="@style/TextAppearance.AppCompat.Headline"/>
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/custom_diag_msg_tv"
        android:layout_marginTop="15dp"
        style="@style/TextAppearance.AppCompat.Subhead"/>

</LinearLayout>

Utils.java

public static void showAlertDialog(Context activityContext, Integer iconResource, String title, String message){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activityContext, R.style.Theme_MaterialComponents_DayNight_Dialog_Alert);
    LinearLayout customLL = (LinearLayout) LayoutInflater.from(activityContext).inflate(R.layout.custom_dialog_layout, null);
    ImageView icon = customLL.findViewById(R.id.custom_diag_img_view);
    if(iconResource != null)
        icon.setImageResource(iconResource);
    android.widget.TextView titleTV = customLL.findViewById(R.id.custom_diag_title_tv);
    titleTV.setText(title);
    TextView msgTV= customLL.findViewById(R.id.custom_diag_msg_tv);
    msgTV.setText(message);
    dialogBuilder
            .setView(customLL)
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //some code
                }
            })
            .show();
}

MyActivity.java

    //without title
Utils.showAlertDialog(UploadActivity.this,R.drawable.ic_error, null, getString(R.string.allowed_images));

//with title
Utils.showAlertDialog(UploadActivity.this,R.drawable.ic_error, "Some title, getString(R.string.allowed_images));

You can add checkbox or other actions

Khalid Lakhani
  • 179
  • 2
  • 10
0

AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

            alert.setMessage("clicked button");
            alert.setTitle("Your Title"); 
            alert.setIcon(R.drawable.info);  
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

            alert.create().show();

if you want to show icon , firstly you must have a title