Created
November 1, 2013 18:46
-
-
Save snadjafi/7269963 to your computer and use it in GitHub Desktop.
Custom DialogFragment
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 com.kuapay.library.fragment; | |
import android.app.Activity; | |
import android.app.Dialog; | |
import android.os.Bundle; | |
import android.support.v4.app.DialogFragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.Window; | |
import android.widget.Button; | |
import android.widget.RelativeLayout; | |
import android.widget.TextView; | |
import com.kuapay.library.R; | |
import com.kuapay.library.interfaces.KuaCustomDialogFragmentInterface; | |
import com.kuapay.library.util.Kualog; | |
public class KuaCustomDialogFragment extends DialogFragment | |
{ | |
//region Variables | |
private View dividerView; | |
private TextView titleTextView; | |
private TextView bodyTextView; | |
private Button negativeButton; | |
private Button positiveButton; | |
private RelativeLayout buttonsRelativeLayout; | |
private KuaCustomDialogFragmentInterface dialogCoordinator; | |
//endregion | |
//region Listeners | |
private View.OnClickListener positiveClickListener = new View.OnClickListener() | |
{ | |
@Override | |
public void onClick(View view) | |
{ | |
dialogCoordinator.onPositiveOrNeutralClicked(KuaCustomDialogFragment.this); | |
} | |
}; | |
private View.OnClickListener negativeClickListener = new View.OnClickListener() | |
{ | |
@Override | |
public void onClick(View view) | |
{ | |
dialogCoordinator.onNegativeClicked(KuaCustomDialogFragment.this); | |
} | |
}; | |
//endregion | |
//region Constructor | |
public static KuaCustomDialogFragment newInstance(String messageBody) | |
{ | |
return newInstance(messageBody, null, null, null, true); | |
} | |
public static KuaCustomDialogFragment newInstance(String messageBody, boolean isCancelable) | |
{ | |
return newInstance(messageBody, null, null, null, isCancelable); | |
} | |
public static KuaCustomDialogFragment newInstance(String messageBody, String title, boolean isCancelable) | |
{ | |
return newInstance(messageBody, title, null, null, isCancelable); | |
} | |
public static KuaCustomDialogFragment newInstance(String messageBody, String title, String neutralButtonText) | |
{ | |
return newInstance(messageBody, title, null, neutralButtonText, true); | |
} | |
public static KuaCustomDialogFragment newInstance(String messageBody, String title, String neutralButtonText, boolean isCancelable) | |
{ | |
return newInstance(messageBody, title, null, neutralButtonText, isCancelable); | |
} | |
public static KuaCustomDialogFragment newInstance(String messageBody, String title, String negativeButtonText, String positiveButtonText) | |
{ | |
return newInstance(messageBody, title, negativeButtonText, positiveButtonText, true); | |
} | |
public static KuaCustomDialogFragment newInstance(String messageBody, String title, String negativeButtonText, String positiveButtonText, boolean isCancelable) | |
{ | |
KuaCustomDialogFragment mCustomDialog = new KuaCustomDialogFragment(); | |
Bundle args = new Bundle(); | |
args.putString("body", messageBody); | |
args.putBoolean("cancelable",isCancelable); | |
if (title!= null) | |
args.putString("title", title); | |
if (negativeButtonText == null && positiveButtonText != null) | |
args.putString("neutral", positiveButtonText); | |
if (negativeButtonText != null && positiveButtonText != null) | |
{ | |
args.putString("negative", negativeButtonText); | |
args.putString("positive", positiveButtonText); | |
} | |
mCustomDialog.setArguments(args); | |
return mCustomDialog; | |
} | |
//endregion | |
@Override | |
public void onAttach(Activity activity) | |
{ | |
super.onAttach(activity); | |
if(getParentFragment() instanceof KuaCustomDialogFragmentInterface) | |
dialogCoordinator = (KuaCustomDialogFragmentInterface) getParentFragment(); | |
else if (activity instanceof KuaCustomDialogFragmentInterface) | |
dialogCoordinator = (KuaCustomDialogFragmentInterface) activity; | |
else | |
throw new ClassCastException("Parent container must implement KuaCustomDialogFragmentInterface"); | |
} | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) | |
{ | |
Dialog mDialog = new Dialog(getActivity(),android.R.style.Theme_Translucent); | |
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); | |
mDialog.setContentView(R.layout.fragment_dialog_layout); | |
bindUIElements(mDialog); | |
setUpListeners(); | |
mDialog.setCancelable(getArguments().getBoolean("cancelable")); | |
setDialogBody(getArguments().getString("body")); | |
setDialogTitle(getArguments().getString("title")); | |
if (getArguments().getString("neutral") != null) | |
setNeutralButtonText(getArguments().getString("neutral")); | |
else | |
setNegativePositiveButtonText(getArguments().getString("negative"), getArguments().getString("positive")); | |
return mDialog; | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) | |
{ | |
dialogCoordinator.setUpKuaDialog(this); | |
return super.onCreateView(inflater, container, savedInstanceState); | |
} | |
public void setDialogTitle(String title) | |
{ | |
if (title != null && !title.equals("")) | |
{ | |
titleTextView.setVisibility(View.VISIBLE); | |
titleTextView.setText(title); | |
} | |
else | |
titleTextView.setVisibility(View.GONE); | |
} | |
public void setNegativePositiveButtonText(String negativeText, String positiveText) | |
{ | |
if (negativeText != null && !negativeText.equals("") | |
&& positiveText != null && !positiveText.equals("")) | |
{ | |
buttonsRelativeLayout.setVisibility(View.VISIBLE); | |
dividerView.setVisibility(View.VISIBLE); | |
negativeButton.setVisibility(View.VISIBLE); | |
negativeButton.setText(negativeText); | |
positiveButton.setText(positiveText); | |
} | |
} | |
public void setNeutralButtonText(String neutralText) | |
{ | |
if (neutralText != null && !neutralText.equals("")) | |
{ | |
buttonsRelativeLayout.setVisibility(View.VISIBLE); | |
dividerView.setVisibility(View.GONE); | |
negativeButton.setVisibility(View.GONE); | |
positiveButton.setText(neutralText); | |
} | |
} | |
private void setUpListeners() | |
{ | |
positiveButton.setOnClickListener(positiveClickListener); | |
negativeButton.setOnClickListener(negativeClickListener); | |
} | |
private void bindUIElements(Dialog mDialog) | |
{ | |
titleTextView = (TextView)(mDialog.findViewById(R.id.title_textView)); | |
bodyTextView = (TextView) mDialog.findViewById(R.id.dialog_body_textView); | |
negativeButton = (Button) mDialog.findViewById(R.id.dialog_negative_button); | |
positiveButton = (Button) mDialog.findViewById(R.id.dialog_positive_button); | |
buttonsRelativeLayout = (RelativeLayout) mDialog.findViewById(R.id.dialog_buttons_relativeLayout); | |
dividerView = mDialog.findViewById(R.id.dialog_divider_view); | |
} | |
private void setDialogBody(String body) | |
{ | |
bodyTextView.setText(body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment