How do I set the ImageView in Android Dialogs?
20,715
Solution 1
AlertDialog.Builder ImageDialog = new AlertDialog.Builder(MainActivity.this);
ImageDialog.setTitle("Title");
ImageView showImage = new ImageView(MainActivity.this);
ImageDialog.setView(showImage);
ImageDialog.setNegativeButton("ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
}
});
ImageDialog.show();
Solution 2
For setting image in an alert dialog box you need to create a custom dialog like this
dialog.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"
android:orientation="vertical">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/my_image"/>
</LinearLayout>
Then in your activity show your custom dialog box like this
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog, null);
builder.setView(dialogView)
.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
Solution 3
hey follow this link..
First you have to set your custom dialog layout to your dialog box, like below.
setcontentview(R.layout.custom)
and set the image with setImageResources(your image id)
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Solution 4
I use this simple method for showing AlertDialog:
private void showAlertDialog(Context mContext, String mTitle, String mBody, int mImage){
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setCancelable(true);
builder.setIcon(mImage);
if(mTitle.length()>0)
builder.setTitle(mTitle);
if(mBody.length()>0)
builder.setTitle(mBody);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
call method:
showAlertDialog(mContext, "OOPS!", getString(R.string.massage_nointernet), R.drawable.ic_no_internet);
Author by
Vigneshwaran T
Updated on May 03, 2020Comments
-
Vigneshwaran T about 4 years
I am trying to display an image in an android dialog. If I select an image from the gallery app it should be displayed in a dialog. I have tried to fetch the selected image from the gallery app and passed its path to an alert dialog.
-
Vigneshwaran T about 8 yearsi will tried the above sample but i need to set image ,from galley not drawble
-
Narendra Baratam about 8 yearstell clearly bro..like in whats app you want..@VigneshwaranT
-
Vigneshwaran T about 8 yearsi am developing chat application .. i am trying to do if user want to send image to another user ,user select the image from gallery ,that have i will done, i user clicks the image from galley , selected image will shows in alert dialog with two button likes "Send","Cancel" @Narendra Baratam
-
Narendra Baratam about 8 yearsOh, got it..@VigneshwaranT
-
Narendra Baratam about 8 yearsdid you find your solution
-
Narendra Baratam about 8 yearspost your solution, and accept it as an answer, you will get reputation.@VigneshwaranT
-
Vigneshwaran T about 8 yearsAlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = this.getLayoutInflater(); View dialogView = inflater.inflate(R.layout.imageindialog, null); ImageView imageView1(ImageView)dialogView.findViewById(R.id.goProDialogImage); imageView1.setImageBitmap(BitmapFactory.decodeFile(imagepath)); builder.setView(dialogView);
-
Narendra Baratam about 8 yearshey @VigneshwaranT, not here man..click on add answer button and post this code, and you can see the right answer logo, mark it as answer. Then you will get reputation,..what i mean is if you find your ans by own, then post the ans and mark it.
-
Wookie about 7 yearsYes! This solution is the simplest I've seen yet, and it works well.