user7357013
user7357013

Reputation: 126

How to put an image in AlertDialog?

I have a ListView which displays list of images. When I click an image in that ListView I want to display the image in AlertDialog.

Using the following code I am able to get and display text form TextView in an AletDialog. But I don't know how to display an image in an AlertDialog.

I referred many sources.But nothing helped.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    TextView c = (TextView) view.findViewById(R.id.textViewname);
                    String value = c.getText().toString();

                    ImageView i = (ImageView) view.findViewById(R.id.imageView_temp);

                    new AlertDialog.Builder(uploadpage.this).
                            setTitle("title").
                            setMessage(value).
                            show();

                    dialog.show();
                }
            });

        }

My question is how to display(inside AlertDialog) the particular Image of a ListVIew when it is clicked ? I want to display the image or item which is clicked.

EDIT

01-24 14:31:26.504 17180-17180/com.example.prakash.pix91 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.prakash.pix91, PID: 17180
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
                                                                               at android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:108)
                                                                               at android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:285)
                                                                               at com.example.prakash.pix91.uploadpage$6.onItemClick(uploadpage.java:507)
                                                                               at android.widget.AdapterView.performItemClick(AdapterView.java:310)
                                                                               at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
                                                                               at android.widget.AbsListView$PerformClick.run(AbsListView.java:3073)
                                                                               at android.widget.AbsListView$3.run(AbsListView.java:3910)
                                                                               at android.os.Handler.handleCallback(Handler.java:746)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Upvotes: 0

Views: 7441

Answers (3)

Ganpat Kaliya
Ganpat Kaliya

Reputation: 898

You should create your custom layout like this.

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_custom_layout, null);
dialogBuilder.setView(dialogView);

ImageView imageview= (ImageView) dialogView.findViewById(R.id.edittext);
imageview.setImageResource(R.id.image1);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();

That's it.

Upvotes: 0

Ganesh Gudghe
Ganesh Gudghe

Reputation: 1387

Create one layout file for your image and this to your alert dialog

 AlertDialog.Builder   alertdialog = new AlertDialog.Builder(getActivity());
     LayoutInflater inflaterr = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View  viewtemplelayout= inflaterr.inflate(R.layout.imagefile, null);
      ImageView i = (ImageView) viewtemplelayout.findViewById(R.id.imageView_temp);//and set image to image view

         alertdialog.setView(viewtemplelayout);//add your view to alert dilaog
         alertdialog.show()

Upvotes: 2

ucsunil
ucsunil

Reputation: 7494

Your AlertDialog's layout should include an ImageView to display the image. When you instantiate your dialog, you will pass it the image view from the ListView.

ImageView image = new ImageView(getContext());
// Set the resource for the image view
// image.setBitmap(someImageBitmapFromListView);
// You can also set a drawable using setImageResource(Drawable drawable) on the ImageView
AlertDialog aDialog = new AlertDialog.Builder(getActivity())
        .setView(image)
        .setPositiveButton(android.R.string.ok,null)
        .setNegativeButton(android.R.string.cancel, null);
        .create();
aDialog.show();

Extracting the image from the ListView should be simple enough. If you only need to display the image, then the above code should get you started. If you want to display more then take a look at the official guide for creating custom dialogs.

Upvotes: 1

Related Questions