Set Image Icon inside AlertDialog Android Studio Example Tutorial

How to put icon in alert dialog using setIcon() from drawable & mipmap resource directory folder.

Icon represents a particular brand and organization because every app on Google Play Store contain its unique icon and icons is also used in alert dialog box. So here is the complete step by step tutorial for Set Image Icon inside AlertDialog Android Studio Example Tutorial.

android-project-download-code-button

How to Set Image Icon inside AlertDialog Android Studio Example Tutorial.

Code for MainActivity.java file.

package com.android_examples.alertdialog_android_examplescom;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button  button ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new AlertDialog.Builder(MainActivity.this)

                        .setTitle("This is AlertDialog Title")

                        .setIcon(R.mipmap.ic_launcher)

                        .setMessage("This is the message inside AlertDialog.")

                        .setPositiveButton("Submit Button",
                                new DialogInterface.OnClickListener()
                                {
                                    public void onClick(DialogInterface dialoginterface, int a)
                                    {

                                        Toast.makeText(MainActivity.this, "You Clicked On Alert Dialog Submit button", Toast.LENGTH_LONG).show();

                                    }
                                }).show();

            }
        });
    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.alertdialog_android_examplescom.MainActivity"
    android:background="#F3E5F5">

    <Button
        android:text="click here to open alertDialog box with icon and title "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button" />

</RelativeLayout>

Screenshots:

Set Image Icon inside AlertDialog Android Studio Example Tutorial

Click here to download Set Image Icon inside AlertDialog Android Studio Example Tutorial project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *