0

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.

Rachit
  • 3,173
  • 3
  • 28
  • 45
  • What do you mean you want to call onClick? – ashishduh Sep 16 '15 at 18:01
  • I want to attach onClick listeners to the views for Gallery and Camera. When these views are clicked (within the dialog), i want to start intents for the default gallery or camera app. And I want the result from those intents to be returned to my calling fragment – Rachit Sep 16 '15 at 18:07

3 Answers3

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

Change To

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

If This Not Work Then Try

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

OR

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

For startActivityForResult()

IF(getparent() == null) 
{
 startActivityForResult();
}else
{
 getparent().startActivityForResult();
} 
Maulik Santoki
  • 532
  • 4
  • 14
0

You should be using DialogFragment, look at this answer. Here are the two important lines from that answer:

In calling Fragment:

dialog.setTargetFragment(this, YES_NO_CALL);

In DialogFragment:

getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);

Basically, when you call the DialogFragment, you need to call setTargetFragment with a request code. Then in your fragment, you handle the response in onActivityResult.

Community
  • 1
  • 1
ashishduh
  • 6,629
  • 3
  • 30
  • 35
  • I cannot call DialogFragment, because the BottomBaseDialog is a library that I'm using. – Rachit Sep 16 '15 at 18:13
  • Well I'm not sure what that library is, can you call `getTargetFragment()` from it? – ashishduh Sep 16 '15 at 18:14
  • No, the base class extends Dialog, and not DialogFragment – Rachit Sep 16 '15 at 18:17
  • Well then I believe you are forced to implement your own interface. It's quite simple, [look here](http://stackoverflow.com/a/13435204/1199931) – ashishduh Sep 16 '15 at 18:21
  • Yes, I thought of doing this first. But see, I don't want to return something from the Dialog to the Fragment. On the contrary, I want to call a method of my fragment from within the Dialog. How can I do that with interfaces? – Rachit Sep 16 '15 at 18:24
  • I'm unable to cast the interface to the Activity. – Rachit Sep 16 '15 at 19:05
0

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();
}
Rachit
  • 3,173
  • 3
  • 28
  • 45