Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put an image in an AlertDialog? Android

I don't know how to put an image into an AlertDialog.

I have this code, but i think this is not possible.

AlertDialog.Builder alert = new AlertDialog.Builder(MessageDemo.this);     ImageView imageView = (ImageView) findViewById(R.id.imageView1);     imageView.setImageResource(R.drawable.cw);              alert.setView(imageView);     alert.setNeutralButton("Here!", new DialogInterface.OnClickListener() {     public void onClick(DialogInterface dlg, int sumthin) {      } });     alert.show(); 
like image 693
fr4n Avatar asked Jun 08 '11 09:06

fr4n


People also ask

Is AlertDialog deprecated?

Is AlertDialog deprecated? This method is deprecated.

What is the use of AlertDialog in Android?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.

What is the difference between Dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .


2 Answers

Create one sample.xml and add ImageView in that XML.

sample.xml

<?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">       <ImageView         android:id="@+id/dialog_imageview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/ic_launcher" />  </LinearLayout> 

Java Code :

AlertDialog.Builder alertadd = new AlertDialog.Builder(MessageDemo.this); LayoutInflater factory = LayoutInflater.from(MessageDemo.this); final View view = factory.inflate(R.layout.sample, null); alertadd.setView(view); alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {                 public void onClick(DialogInterface dlg, int sumthin) {                  }             });  alertadd.show(); 
like image 138
Niranj Patel Avatar answered Sep 20 '22 13:09

Niranj Patel


You could do it in the following way. This will show an alertDialog with a message (if you don't need the message, just remove that line) and the image (and an OK button):

ImageView image = new ImageView(this); image.setImageResource(R.drawable.YOUR_IMAGE_ID);  AlertDialog.Builder builder =          new AlertDialog.Builder(this).         setMessage("Message above the image").         setPositiveButton("OK", new OnClickListener() {                                  @Override             public void onClick(DialogInterface dialog, int which) {                     dialog.dismiss();             }         }).         setView(image); builder.create().show(); 
like image 22
Miguel Rivero Avatar answered Sep 22 '22 13:09

Miguel Rivero