Search code examples
androidimageviewandroid-alertdialog

show an imageView in a dialog when clicked android


I have an ImageView that its data sets from java.

My problem is: how can I show this Image in a Dialog when I clicked on that ImageView?

I wrote some code but it produces a NullPointerException:

final ImageView imageView = (ImageView)view.findViewById(R.id.profile_img);
imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Dialog settingsDialog = new Dialog(getActivity());
                    ImageView iv= (ImageView) view.findViewById(R.id.profile_img_popup);
                    Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
                    iv.setImageBitmap(bm);
                    settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                    settingsDialog.setContentView(getActivity().getLayoutInflater().inflate(R.layout.image_layout
                            , null));
                    settingsDialog.show();
                }
            });

The Layout file (image_layout.xml) is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/profile_img_popup"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="dismissListener"
        android:text="OK"/>
</LinearLayout>

Logcat output containing the NPE:

08-13 01:46:46.386    2583-2583/com.saniyeh.iwedding E/cutils﹕ to chown(/mnt/shell/emulated/0, 0, 0)
08-13 01:46:46.406    2583-2583/com.saniyeh.iwedding E/cutils﹕ to chown(/mnt/shell/emulated/obb, 0, 0)
08-13 01:46:46.406    2583-2583/com.saniyeh.iwedding E/cutils﹕ to chown(/storage/emulated/0/Android, 0, 0)
08-13 01:46:46.408    2583-2583/com.saniyeh.iwedding E/cutils﹕ to chown(/storage/emulated/0/Android/obb, 0, 0)
08-13 01:46:50.560    2583-2583/com.saniyeh.iwedding E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.saniyeh.iwedding.FragmentProfile$1.onClick(FragmentProfile.java:154)
            at android.view.View.performClick(View.java:4211)
            at android.view.View$PerformClick.run(View.java:17446)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

Solution

  • Try this

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Dialog settingsDialog = new Dialog(getActivity());
    
            LayoutInflater inflater = getLayoutInflater();
            View newView = (View) inflater.inflate(R.layout.image_layout, null);
    
            settingsDialog.setContentView(newView);
            settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    
            ImageView iv= (ImageView) newView.findViewById(R.id.profile_img_popup);
            Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
            iv.setImageBitmap(bm);
    
    
            settingsDialog.show();
    
    
        }
    });