How to put an image in an AlertDialog? Android

℡╲_俬逩灬. 提交于 2020-01-09 02:15:05

问题


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();

回答1:


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();



回答2:


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();



回答3:


There is an another option you can put an image in alert dialog without creating an xml file.

    AlertDialog.Builder builder=new AlertDialog.Builder(DialogActivity.this);
    builder.setCancelable(true);
    builder.setIcon(R.drawable.your image name);
    builder.setTitle("Incoming Call");
    builder.setInverseBackgroundForced(true);
    builder.setPositiveButton("Accept",new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which){
            dialog.dismiss();
        }
    });
    builder.setNegativeButton("Reject",new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which){
            dialog.dismiss();
        }
    });
    AlertDialog alert=builder.create();
    alert.show();



回答4:


For images I just do this:

public void onClick (View view) {
    new AlertDialog.Builder(this).setView(R.layout.your_layout).show();
}



回答5:


In addition to the other answers, i want to add the point that you should not be obliged to use AlertDialog. For example, in my case i want to show only imageView in a dialog and AlertDialog didn't worked for this case. The below solution worked for me :

LayoutInflater factory = LayoutInflater.from(context);
final View view = factory.inflate(R.layout.myphoto_layout, null);
Dialog dialog = new Dialog(context);
dialog.setContentView(view);
dialog.show();

myphoto_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/someImage" />

If you want to add image to your layout dynamically, you can use the below code :

ImageView iv = view.findViewById(R.id.iv); // id of your imageView element
iv.setImageBitmap(itmap);



回答6:


make method

private void dialog() {
    AlertDialog.Builder alert = new AlertDialog.Builder(MainMenuActivity.this);
    LayoutInflater inflater = this.getLayoutInflater();
    View team = inflater.inflate(R.layout.image_layout, null);
    alert.setView(team);
    alert.show();
}

and make layout named image_layout and put any thing in this layout



来源:https://stackoverflow.com/questions/6276501/how-to-put-an-image-in-an-alertdialog-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!