BaseDialogFragment.java
public abstract class BaseDialogFragment extends AppCompatDialogFragment {
public AppCompatDialog dialog;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(getLayoutResource(), null);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onStart() {
super.onStart();
dialog = (AppCompatDialog) getDialog();
if (dialog != null) {
WindowManager windowManager =
(WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
dialog.getWindow().setLayout(width - 75, ViewGroup.LayoutParams.WRAP_CONTENT);
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
dialog.getWindow().setAttributes(params);
dialog.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.dialog_rounded_back));
}
}
protected abstract int getLayoutResource();
@Override
public void show(FragmentManager manager, String tag) {
try {
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commitAllowingStateLoss();
} catch (IllegalStateException e) {
}
}
}
Диалог дочернего фрагмента:
public class InvitationAcceptRejectDialog extends BaseDialogFragment {
public InvitationAcceptRejectDialog() {
}
@Override
protected int getLayoutResource() {
return R.layout.invite_accept_reject_dialog;
}
protected OnDialogClickListener alertListener;
@BindView(R.id.tvDialogTitle)
AppCompatTextView tvDialogTitle;
@BindView(R.id.tvDialogMessage)
AppCompatTextView tvDialogMessage;
int requestCode;
public String dialogTitle;
public String dialogMessage;
public Bundle bundle;
@OnClick({R.id.imgCloseDialog, R.id.btnYes, R.id.btnNo})
public void dialgClick(View view) {
switch (view.getId()) {
case R.id.imgCloseDialog:
break;
case R.id.btnYes:
alertListener.onPositiveClick(dialog, requestCode, bundle);
break;
case R.id.btnNo:
alertListener.onNegativeClick(dialog, requestCode, bundle);
break;
}
dialog.dismiss();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tvDialogTitle.setText(dialogTitle);
tvDialogMessage.setText(dialogMessage);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
public static class Builder {
InvitationAcceptRejectDialog alertDialogFragment;
public Builder() {
alertDialogFragment = new InvitationAcceptRejectDialog();
}
public Builder setTitle(String title) {
alertDialogFragment.dialogTitle = title;
return this;
}
public Builder setMessage(String message) {
alertDialogFragment.dialogMessage = message;
return this;
}
public Builder setBundel(Bundle bundel) {
alertDialogFragment.bundle = bundel;
return this;
}
public Builder setCallback(OnDialogClickListener mListener, int code) {
alertDialogFragment.alertListener = mListener;
alertDialogFragment.requestCode = code;
return this;
}
public InvitationAcceptRejectDialog build() {
return alertDialogFragment;
}
}
}
Реализация в активность или fragmnet :
InvitationAcceptRejectDialog build = new InvitationAcceptRejectDialog.Builder()
.setCallback(this, Constant.DialogConstant.ACCEPET_INVITE)
.setTitle(getString(R.string.logout))
.setMessage(getString(R.string.logout_message))
.build();
build.show(getSupportFragmentManager(), "TAG");
Интерфейс для ручки положительного и отрицательного нажатия кнопки:
public interface OnDialogClickListener {
void onPositiveClick(DialogInterface dialog, int id, Bundle bundle);
void onNegativeClick(DialogInterface dialog, int id, Bundle bundle);
}