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();
Is AlertDialog deprecated? This method is deprecated.
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.
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 .
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With