-1

Is it possible to create a custom dialog on Android depicting stages, like three images, as each stage is achieved, the image changes color, until all images change color and the dialog closes. I also want an opportunity for a user to cancel via a cancel button. To accomplish this I need to have the app communicate to the dialog box, and for the dialog box to be able to communicate with the calling fragment. I know the latter is true, but can the fragment communicate with the dialog beyond opening it? Is there a good example of this?

lcj
  • 1,355
  • 16
  • 37

1 Answers1

0

You can create your own dialog with UI that you expect and there are many ways to achieve that. I prefer following way with DialogFragment, an convenience dialog class from Android framework:

public class SampleDialog extends DialogFragment {
    public SampleDialog(){
        super();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
         View view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_your_dialog_layout, null);//your custom layout
         Button btnYes = view.findViewById(R.id.btnYes); //assume the customized layout have two buttons (yes and no)
         Button btnNo = view.findViewById(R.id.btnNo);
         btnNo.setOnClickListener(view->{
             //your business code
             dismiss();
         })
         AlertDialog alertD = builder.create();
         alertD.setView(view);
         return alertD;
    }
}

Then, call dialog from activity:

SampleDialog dialog = new SampleDialog();
dialog.show(getSupportFragmentManager(),"tag");

If you want to call dialog from fragment:

SampleDialog dialog = new SampleDialog();
dialog.show(getActivity().getSupportFragmentManager(),"tag"); 

Refer to this link to see more DialogFragment methods: https://developer.android.com/reference/android/app/DialogFragment

UPDATE:

To pass data from dialog to activity, simply use interface, I found a good guide: https://github.com/codepath/android_guides/wiki/Using-DialogFragment#passing-data-to-activity

TaQuangTu
  • 2,155
  • 2
  • 16
  • 30
  • I have the UI and will call it. Is there a way to communicate with it from the fragment while it is open? – lcj Mar 03 '20 at 00:19
  • @lcj To update business task on dialog's holder (a fragment or an activity), the dialog must keep an instance of it's holder. I usually use interface to do that clearly. Let me update my answer – TaQuangTu Mar 03 '20 at 08:48