Search code examples
androidandroid-fragmentscustomdialog

Android: Call onClick from fragment/activity for views within Custom Dialog


I've created a custom dialog, which has multiple views within it. On click of these Views, I would like to start activities for results, like Camera, Gallery, etc.

CustomDialog

public class CustomDialog extends BottomBaseDialog {
    public static LinearLayout ll_camera;
    public static LinearLayout ll_gallery;

    public CustomDialog(Context context, View animateView) {
        super(context, animateView);
    }

    @Override
    public View onCreateView() {
        View inflate = View.inflate(context, R.layout.dialog_custom, null);

        ll_camera = ViewFindUtils.find(inflate, R.id.camera_linear_layout);
        ll_gallery = ViewFindUtils.find(inflate, R.id.gallery_linear_layout);

        return inflate;
    }

    @Override
    public boolean setUiBeforShow() {
        ll_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // OPEN INTENT FOR CAMERA
                dismiss();
            }
        });
        ll_gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // OPEN INTENT FOR GALLERY
                dismiss();
            }
        });

        return false;
    }
}

Then within my fragment, I've displayed this dialog by

IOSTaoBaoDialog dialog = new IOSTaoBaoDialog(getActivity(), AddActivity.drawerLayout);
dialog.show();

How can I call onClick for the Camera and Gallery Linear Layout views from within my Fragment? I also need to get the result of the activities back into the fragment, so I can process it. Please suggest.

I've done a lot of search and I came across suggestions to use Interfaces, however, I do not clearly understand how that will work.


Solution

  • I figured out the solution for this, for if somebody else also gets stuck in the same situation:

    I passed the instance of my calling fragment to the dialog. Then from within the dialog, I called the fragment.startActivityForResult() method. So when the result was received, it was sent to the onActivityResult() method of the fragment.

    The code is:

    Dialog:

    public class SelectApplicationDialog extends BottomBaseDialog {
        public static LinearLayout ll_camera;
        public static LinearLayout ll_gallery;
    
        Fragment fragment;
    
        public SelectApplicationDialog(Context context, View animateView, Fragment fragment) {
            super(context, animateView);
            this.fragment = fragment;
        }
    
        @Override
        public View onCreateView() {
            View inflate = View.inflate(context, R.layout.dialog_select_application, null);
    
            ll_camera = ViewFindUtils.find(inflate, R.id.camera_linear_layout);
    
            ll_gallery = ViewFindUtils.find(inflate, R.id.gallery_linear_layout);
    
            return inflate;
        }
    
        @Override
        public boolean setUiBeforShow() {
            ll_camera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    File externalStorageFile = new File(imagePath);
    
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    AddCourseFragment.imageUri = Uri.fromFile(externalStorageFile);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, AddCourseFragment.imageUri);
                    fragment.startActivityForResult(intent, 1);
                    dismiss();
                }
            });
            ll_gallery.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    fragment.startActivityForResult(pickPhoto, 2);
                    dismiss();
                }
            });
    
            return false;
        }
    }
    

    Calling Fragment:

    public void openUploadImageDialog() {
        SelectApplicationDialog dialog = new SelectApplicationDialog(getContext(), 
                AddActivity.addLinearLayout, AddCourseFragment.this);
        dialog.show();
    }