Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pxl.pxlbeamapp.util;
- import android.content.Context;
- import android.content.res.Configuration;
- import android.support.v7.app.AlertDialog;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.Button;
- import android.widget.TextView;
- import com.pxl.pxlbeamapp.R;
- import com.pxl.pxlbeamapp.util.view.ViewUtils;
- public class IOSAlertDialog {
- private static final int THREE_RATIO = 3;
- private static final int TWO_RATIO = 2;
- private AlertDialog dialog;
- private String title;
- private String message;
- private String leftButtonText;
- private String rightButtonText;
- private Runnable leftButtonListener;
- private Runnable rightButtonListener;
- private boolean isCancelable;
- public static Builder newBuilder(Context context) {
- return new IOSAlertDialog().new Builder(context);
- }
- public void show() {
- if (dialog != null && dialog.getWindow() != null) {
- int customDialogWidth;
- if (dialog.getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
- customDialogWidth = (ViewUtils.getScreenWidth(dialog.getWindow()) / THREE_RATIO) * TWO_RATIO;
- } else {
- customDialogWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
- }
- dialog.show();
- dialog.getWindow().setLayout(customDialogWidth, WindowManager.LayoutParams.WRAP_CONTENT);
- }
- }
- @SuppressWarnings("unused")
- public class Builder {
- private Context context;
- private Builder(Context context) {
- //clear fields
- IOSAlertDialog.this.isCancelable = true;
- IOSAlertDialog.this.title = null;
- IOSAlertDialog.this.message = null;
- IOSAlertDialog.this.leftButtonText = null;
- IOSAlertDialog.this.rightButtonText = null;
- IOSAlertDialog.this.leftButtonListener = null;
- IOSAlertDialog.this.rightButtonListener = null;
- IOSAlertDialog.this.dialog = null;
- this.context = context;
- }
- public Builder addTitle(String title) {
- IOSAlertDialog.this.title = title;
- return this;
- }
- public Builder setCancelable(boolean isCancelable) {
- IOSAlertDialog.this.isCancelable = isCancelable;
- return this;
- }
- public Builder addMessage(String message) {
- IOSAlertDialog.this.message = message;
- return this;
- }
- public Builder addLeftButtonText(String leftButtonText) {
- IOSAlertDialog.this.leftButtonText = leftButtonText;
- return this;
- }
- public Builder addRightButtonText(String rightButtonText) {
- IOSAlertDialog.this.rightButtonText = rightButtonText;
- return this;
- }
- public Builder addLeftButtonListener(Runnable leftListener) {
- IOSAlertDialog.this.leftButtonListener = leftListener;
- return this;
- }
- public Builder addRightButtonListener(Runnable rightListener) {
- IOSAlertDialog.this.rightButtonListener = rightListener;
- return this;
- }
- public IOSAlertDialog build() {
- AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.CustomAlertDialog);
- //Get alert view
- View view = LayoutInflater.from(context).inflate(R.layout.alert_dialog, null);
- //Get child views
- Button leftBtn = view.findViewById(R.id.btn_left);
- Button rightBtn = view.findViewById(R.id.btn_right);
- View delimiterVertical = view.findViewById(R.id.delimiter_vertical);
- TextView tvTitle = view.findViewById(R.id.tv_title);
- TextView tvMessage = view.findViewById(R.id.tv_message);
- //Initialize child views if required
- tvTitle.setText(IOSAlertDialog.this.title == null ? "" : IOSAlertDialog.this.title);
- tvMessage.setText(IOSAlertDialog.this.message == null ? "" : IOSAlertDialog.this.message);
- leftBtn.setText(IOSAlertDialog.this.leftButtonText == null ? "" : IOSAlertDialog.this.leftButtonText);
- rightBtn.setText(IOSAlertDialog.this.rightButtonText == null ? "" : IOSAlertDialog.this.rightButtonText);
- leftBtn.setOnClickListener(v -> processClick(leftButtonListener));
- rightBtn.setOnClickListener(v -> processClick(rightButtonListener));
- //adopt ui for two buttons style or one button style
- boolean isOneButtonStyle = IOSAlertDialog.this.rightButtonText == null && IOSAlertDialog.this.rightButtonListener == null;
- int visibility = isOneButtonStyle ? View.GONE : View.VISIBLE;
- delimiterVertical.setVisibility(visibility);
- rightBtn.setVisibility(visibility);
- builder.setView(view);
- builder.setCancelable(IOSAlertDialog.this.isCancelable);
- //rebuild
- dialog = builder.create();
- return IOSAlertDialog.this;
- }
- private void processClick(Runnable runnable) {
- if (dialog != null && dialog.isShowing()) {
- dialog.dismiss();
- } else {
- Log.e(IOSAlertDialog.class.getSimpleName(), "Dialog is null or was already dismissed)");
- }
- if (runnable != null) {
- runnable.run();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement