Created
October 29, 2015 22:30
-
-
Save AnirudhaS/fa77f723ac20f46ac5bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class YesNoDialog extends DialogFragment { | |
| String message; | |
| String title; | |
| String yesText; | |
| String noText; | |
| /* The activity that creates an instance of this dialog fragment must | |
| * implement this interface in order to receive event callbacks. | |
| * Each method passes the DialogFragment in case the host needs to query it. */ | |
| public interface YesNoListener { | |
| public void onYesClick(DialogFragment dialog); | |
| public void onNoClick(DialogFragment dialog); | |
| } | |
| // Use this instance of the interface to deliver action events | |
| YesNoListener mListener; | |
| // Override the Fragment.onAttach() method to instantiate the YesNoListener | |
| // @Override | |
| public void onAttach(Activity activity) { | |
| super.onAttach(activity); | |
| // Verify that the host activity implements the callback interface | |
| try { | |
| // Instantiate the YesNoListener so we can send events to the host | |
| mListener = (YesNoListener) activity; | |
| } catch (ClassCastException e) { | |
| // The activity doesn't implement the interface, throw exception | |
| throw new ClassCastException(activity.toString() | |
| + " must implement YesNoListener"); | |
| } | |
| } | |
| @Override | |
| public Dialog onCreateDialog(Bundle savedInstanceState) { | |
| // Get the bundle arguements. | |
| Bundle bundle = this.getArguments(); | |
| this.message = bundle.getString("message"); | |
| this.title = bundle.getString("title"); | |
| this.yesText = bundle.getString("yesText"); | |
| this.noText = bundle.getString("noText"); | |
| // Build the dialog and set up the button click handlers | |
| AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
| builder.setMessage(this.message) | |
| .setPositiveButton(this.yesText, new DialogInterface.OnClickListener() { | |
| public void onClick(DialogInterface dialog, int id) { | |
| // Send the positive button event back to the host activity | |
| mListener.onYesClick(YesNoDialog.this); | |
| } | |
| }) | |
| .setNegativeButton(this.noText, new DialogInterface.OnClickListener() { | |
| public void onClick(DialogInterface dialog, int id) { | |
| // Send the negative button event back to the host activity | |
| mListener.onNoClick(YesNoDialog.this); | |
| } | |
| }); | |
| return builder.create(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment