Skip to content

Instantly share code, notes, and snippets.

@milaptank
Created March 6, 2018 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milaptank/b1385d48a222c6ebc7425c30cc1d5272 to your computer and use it in GitHub Desktop.
Save milaptank/b1385d48a222c6ebc7425c30cc1d5272 to your computer and use it in GitHub Desktop.
package com.whyte.android.whytecatalogueapp.fragment;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.TextView;
import com.core.util.BaseUtils;
import com.core.util.Debug;
import com.whyte.android.whytecatalogueapp.R;
import com.whyte.android.whytecatalogueapp.helper.OnDialogClickListener;
/**
* @author Milap Tank
* Email:milaptank@gmail.com
* @desc BaseDialogFragment.java is for
* @since 24/09/17 12:07 PM
*/
public abstract class BaseDialogFragment extends DialogFragment implements
View.OnClickListener , TextView.OnEditorActionListener{
AlertDialog dialog;
String callbackMsg = "Missing Callback";
protected String positiveText;
protected String negativeText;
protected boolean cancelable = true;
protected int dialogId;
protected OnDialogClickListener alertListener;
public String errorMessage;
public String titleText;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), R.style.AppTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = getActivity().getLayoutInflater().inflate(
getFragmentLayoutId(), null);
bindView(view);
builder.setView(view);
builder.setPositiveButton(positiveText, null);
builder.setNegativeButton(negativeText, null);
builder.setCancelable(cancelable);
setCancelable(cancelable);
return builder.create();
}
@Override
public void onStart() {
super.onStart();
dialog = (AlertDialog) getDialog();
Debug.e("BaseDialogFragment", "Line no: 68,Method: onStart: ");
Button posButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
posButton.setTag(Dialog.BUTTON_POSITIVE);
posButton.setOnClickListener(this);
posButton.setTextColor(getActivity().getResources().getColor(R.color.colorPrimary));
Button negButton = dialog.getButton(Dialog.BUTTON_NEGATIVE);
negButton.setTag(Dialog.BUTTON_NEGATIVE);
negButton.setOnClickListener(this);
negButton.setTextColor(getActivity().getResources().getColor(R.color.colorPrimary));
}
public abstract int getFragmentLayoutId();
public abstract void bindView(View view);
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
switch ((int) v.getTag()) {
case Dialog.BUTTON_POSITIVE:
if (alertListener == null) {
BaseUtils.showToast(getActivity(), callbackMsg);
} else {
alertListener.onPositiveClick(dialog, dialogId, null);
}
break;
case Dialog.BUTTON_NEGATIVE:
if (alertListener == null)
BaseUtils.showToast(
getActivity(), callbackMsg);
else
alertListener.onNegativeClick(dialog, dialogId, null);
break;
}
}
/**
* Called when an action is being performed.
*
* @param v The view that was clicked.
* @param actionId Identifier of the action. This will be either the
* identifier you supplied, or {@link EditorInfo#IME_NULL
* EditorInfo.IME_NULL} if being called due to the enter key
* being pressed.
* @param event If triggered by an enter key, this is the event;
* otherwise, this is null.
* @return Return true if you have consumed the action, else false.
*/
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return true;
}
return false;
}
@Override
public void onStop() {
super.onStop();
Debug.e("BaseDialogFragment", "Line no: 125,Method: onStop: ");
}
@Override
public void onPause() {
super.onPause();
Debug.e("BaseDialogFragment", "Line no: 132,Method: onPause: ");
}
@Override
public void onDetach() {
super.onDetach();
Debug.e("BaseDialogFragment", "Line no: 140,Method: onDetach: ");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Debug.e("BaseDialogFragment", "Line no: 179,Method: onDestroyView: ");
}
@Override
public void onDestroy() {
super.onDestroy();
Debug.e("BaseDialogFragment", "Line no: 152,Method: onDestroy: ");
}
@Override
public void onResume() {
super.onResume();
Debug.e("BaseDialogFragment", "Line no: 159,Method: onResume: ");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Debug.e("BaseDialogFragment", "Line no: 167,Method: onCreateView: ");
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Debug.e("BaseDialogFragment", "Line no: 175,Method: onActivityCreated: ");
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Debug.e("BaseDialogFragment", "Line no: 182,Method: onAttach: ");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Debug.e("BaseDialogFragment", "Line no: 188,Method: onCreate: ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment