Created
February 27, 2016 12:31
-
-
Save dmdevgo/67081f33318ac7bc30d2 to your computer and use it in GitHub Desktop.
This file contains 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
package dmdevgo.dialog; | |
import android.app.Activity; | |
import android.app.Dialog; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v4.app.DialogFragment; | |
import android.support.v4.app.Fragment; | |
import android.support.v7.app.AlertDialog; | |
import java.io.Serializable; | |
/** | |
* @author Dmitriy Gorbunov | |
*/ | |
public class AlertDialogFragment extends BaseDialogFragment { | |
private static final String ARG_TITLE = "arg_title"; | |
private static final String ARG_MESSAGE = "arg_message"; | |
private static final String ARG_POSITIVE_BUTTON = "arg_positive_button"; | |
private static final String ARG_NEGATIVE_BUTTON = "arg_negative_button"; | |
private static final String ARG_NEUTRAL_BUTTON = "arg_neutral_button"; | |
private static final String ARG_DIALOG_TAG = "arg_dialog_tag"; | |
private OnButtonsClickListener mButtonsClickListener; | |
private OnDismissListener mDismissListener; | |
public String getDialogTagAsString() { | |
return (String) getDialogTag(); | |
} | |
public interface OnButtonsClickListener { | |
void onPositiveButtonClick(AlertDialogFragment dialog); | |
void onNegativeButtonClick(AlertDialogFragment dialog); | |
void onNeutralButtonClick(AlertDialogFragment dialog); | |
} | |
public interface OnDismissListener { | |
void onDismissDialog(AlertDialogFragment dialog); | |
} | |
public DialogFragment setTargetFragment(Fragment fragment) { | |
super.setTargetFragment(fragment, 0); | |
return this; | |
} | |
public Serializable getDialogTag() { | |
return getArguments().getSerializable(ARG_DIALOG_TAG); | |
} | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
if (getTargetFragment() instanceof OnButtonsClickListener) { | |
mButtonsClickListener = (OnButtonsClickListener) getTargetFragment(); | |
} else if (getParentFragment() instanceof OnButtonsClickListener) { | |
mButtonsClickListener = (OnButtonsClickListener) getParentFragment(); | |
} else if (getActivity() instanceof OnButtonsClickListener) { | |
mButtonsClickListener = (OnButtonsClickListener) getActivity(); | |
} | |
if (getTargetFragment() instanceof OnDismissListener) { | |
mDismissListener = (OnDismissListener) getTargetFragment(); | |
} else if (getParentFragment() instanceof OnDismissListener) { | |
mDismissListener = (OnDismissListener) getParentFragment(); | |
} else if (getActivity() instanceof OnDismissListener) { | |
mDismissListener = (OnDismissListener) getActivity(); | |
} | |
} | |
@NonNull | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
if (getArguments().containsKey(ARG_TITLE)) { | |
builder.setTitle(getArguments().getCharSequence(ARG_TITLE)); | |
} | |
if (getArguments().containsKey(ARG_MESSAGE)) { | |
builder.setMessage(getArguments().getCharSequence(ARG_MESSAGE)); | |
} | |
if (getArguments().containsKey(ARG_POSITIVE_BUTTON)) { | |
builder.setPositiveButton(getArguments().getCharSequence(ARG_POSITIVE_BUTTON), mInnerButtonsClickListener); | |
} | |
if (getArguments().containsKey(ARG_NEGATIVE_BUTTON)) { | |
builder.setNegativeButton(getArguments().getCharSequence(ARG_NEGATIVE_BUTTON), mInnerButtonsClickListener); | |
} | |
if (getArguments().containsKey(ARG_NEUTRAL_BUTTON)) { | |
builder.setNeutralButton(getArguments().getCharSequence(ARG_NEUTRAL_BUTTON), mInnerButtonsClickListener); | |
} | |
return builder.create(); | |
} | |
@Override | |
public void onDismiss(DialogInterface dialog) { | |
super.onDismiss(dialog); | |
if (mDismissListener != null) { | |
mDismissListener.onDismissDialog(AlertDialogFragment.this); | |
} | |
} | |
private DialogInterface.OnClickListener mInnerButtonsClickListener = new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
if (mButtonsClickListener != null) { | |
switch (which) { | |
case DialogInterface.BUTTON_POSITIVE: | |
mButtonsClickListener.onPositiveButtonClick(AlertDialogFragment.this); | |
break; | |
case DialogInterface.BUTTON_NEGATIVE: | |
mButtonsClickListener.onNegativeButtonClick(AlertDialogFragment.this); | |
break; | |
case DialogInterface.BUTTON_NEUTRAL: | |
mButtonsClickListener.onNeutralButtonClick(AlertDialogFragment.this); | |
break; | |
} | |
} | |
dismissAllowingStateLoss(); | |
} | |
}; | |
public static class Builder { | |
private Context mContext; | |
private Bundle mArgs = new Bundle(); | |
public Builder(Context context) { | |
mContext = context; | |
} | |
public Builder setTitle(int titleId) { | |
mArgs.putCharSequence(ARG_TITLE, mContext.getText(titleId)); | |
return this; | |
} | |
public Builder setTitle(CharSequence title) { | |
mArgs.putCharSequence(ARG_TITLE, title); | |
return this; | |
} | |
public Builder setMessage(int messageId) { | |
mArgs.putCharSequence(ARG_MESSAGE, mContext.getText(messageId)); | |
return this; | |
} | |
public Builder setMessage(CharSequence message) { | |
mArgs.putCharSequence(ARG_MESSAGE, message); | |
return this; | |
} | |
public Builder setPositiveButton(int positiveButtonId) { | |
mArgs.putCharSequence(ARG_POSITIVE_BUTTON, mContext.getText(positiveButtonId)); | |
return this; | |
} | |
public Builder setPositiveButton(CharSequence positiveButton) { | |
mArgs.putCharSequence(ARG_POSITIVE_BUTTON, positiveButton); | |
return this; | |
} | |
public Builder setNegativeButton(int negativeButtonId) { | |
mArgs.putCharSequence(ARG_NEGATIVE_BUTTON, mContext.getText(negativeButtonId)); | |
return this; | |
} | |
public Builder setNegativeButton(CharSequence negativeButton) { | |
mArgs.putCharSequence(ARG_NEGATIVE_BUTTON, negativeButton); | |
return this; | |
} | |
public Builder setNeutralButton(int neutralButtonId) { | |
mArgs.putCharSequence(ARG_NEUTRAL_BUTTON, mContext.getText(neutralButtonId)); | |
return this; | |
} | |
public Builder setNeutralButton(CharSequence neutralButton) { | |
mArgs.putCharSequence(ARG_NEUTRAL_BUTTON, neutralButton); | |
return this; | |
} | |
public Builder setTag(Serializable tag) { | |
mArgs.putSerializable(ARG_DIALOG_TAG, tag); | |
return this; | |
} | |
public AlertDialogFragment create() { | |
AlertDialogFragment alertDialogFragment = new AlertDialogFragment(); | |
alertDialogFragment.setArguments(mArgs); | |
return alertDialogFragment; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment