|
| 1 | +package com.codepath.libraries.androidviewhelpers; |
| 2 | + |
| 3 | + |
| 4 | +import android.app.AlertDialog; |
| 5 | +import android.app.Dialog; |
| 6 | +import android.content.DialogInterface; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.support.v4.app.DialogFragment; |
| 9 | +import android.support.v4.app.FragmentActivity; |
| 10 | + |
| 11 | +public class SimpleAlertDialog extends DialogFragment { |
| 12 | + SimpleAlertListener listener; |
| 13 | + String alertTitle, positiveText, negativeText; |
| 14 | + FragmentActivity activity; |
| 15 | + |
| 16 | + // SimpleAlertDialog.build(activity, "Do you want to delete that?", |
| 17 | + // "OK", "Cancel", ...).show(); |
| 18 | + public static SimpleAlertDialog build(FragmentActivity activity, String body, |
| 19 | + String positiveText, String negativeText, |
| 20 | + SimpleAlertListener listener) { |
| 21 | + SimpleAlertDialog dialogAlert = new SimpleAlertDialog(); |
| 22 | + dialogAlert.setAlertTitle(body); |
| 23 | + dialogAlert.setAlertListener(listener); |
| 24 | + dialogAlert.setAlertActivity(activity); |
| 25 | + dialogAlert.setAlertButtonText(positiveText, negativeText); |
| 26 | + return dialogAlert; |
| 27 | + } |
| 28 | + |
| 29 | + // SimpleAlertDialog.build(manager, "Do you want to delete that?"); |
| 30 | + public static SimpleAlertDialog build(FragmentActivity activity, String body, |
| 31 | + SimpleAlertListener listener) { |
| 32 | + return build(activity, body, "OK", "Cancel", listener); |
| 33 | + } |
| 34 | + |
| 35 | + public void show() { |
| 36 | + this.show(activity.getSupportFragmentManager(), null); |
| 37 | + } |
| 38 | + |
| 39 | + protected void setAlertTitle(String title) { |
| 40 | + this.alertTitle = title; |
| 41 | + } |
| 42 | + |
| 43 | + protected void setAlertListener(SimpleAlertListener listener) { |
| 44 | + this.listener = listener; |
| 45 | + } |
| 46 | + |
| 47 | + protected void setAlertActivity(FragmentActivity activity) { |
| 48 | + this.activity = activity; |
| 49 | + } |
| 50 | + |
| 51 | + protected void setAlertButtonText(String positiveText, String negativeText) { |
| 52 | + this.positiveText = positiveText; |
| 53 | + this.negativeText = negativeText; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 58 | + AlertDialog.Builder builder = |
| 59 | + new AlertDialog.Builder(getActivity()) |
| 60 | + .setIcon(android.R.drawable.ic_popup_reminder) |
| 61 | + .setTitle(alertTitle); |
| 62 | + if (positiveText != null) { |
| 63 | + builder.setPositiveButton(positiveText, |
| 64 | + new DialogInterface.OnClickListener() { |
| 65 | + public void onClick(DialogInterface dialog, |
| 66 | + int whichButton) { |
| 67 | + listener.onPositive(); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + if (negativeText != null) { |
| 73 | + builder.setNegativeButton(negativeText, |
| 74 | + new DialogInterface.OnClickListener() { |
| 75 | + public void onClick(DialogInterface dialog, |
| 76 | + int whichButton) { |
| 77 | + listener.onNegative(); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + return builder.create(); |
| 83 | + } |
| 84 | + |
| 85 | + public interface SimpleAlertListener { |
| 86 | + public void onPositive(); |
| 87 | + public void onNegative(); |
| 88 | + } |
| 89 | +} |
0 commit comments