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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.