Display an image in AlertDialog

In this blog post, I would like to explain how to display an image in AlertDialog box in Android.

First, create a activity_main.xml XML file, and add the following code:

<?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="horizontal" >

   <Button
        android:id = "@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center">
   </Button>

</LinearLayout>

Then, create a dialog_main.xml, and add folowing code:

<?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="horizontal" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
     <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
</LinearLayout>

Now, create a MainActivity.class Java file, and add following code:

package com.example.aletdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity {
	AlertDialog alert;
	Button button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
        
		
	button = (Button) findViewById(R.id.button);
	button.setText("Click to get a DialogBox");
	button.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			alert();
		}
	});
		
	}
	
	
public void alert(){

	// TODO Auto-generated method stub
AlertDialog.Builder alertadd = new AlertDialog.Builder(
                MainActivity.this);
alertadd.setTitle("Android");

LayoutInflater factory = LayoutInflater.from(MainActivity.this);
final View view = factory.inflate(R.layout.dialog_main, null);

ImageView image= (ImageView) view.findViewById(R.id.imageView);
image.setImageResource(R.drawable.ic_launcher);

TextView text= (TextView) view.findViewById(R.id.textView);
text.setText("Android");

alertadd.setView(view);
alertadd.setNeutralButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dlg, int sumthin) {

                }
            });

alertadd.show();

}
	
}

Screenshot

Screenshot_2014-04-30-18-16-59

Screenshot_2014-04-30-18-17-04

Congrats, you have sucessfully created Dialogbox with the images in Android.
Hope this blog post was helpful. Do let me know your feedback.

Leave a Reply

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