|
| 1 | +package com.lge.qcircle.template; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.app.Activity; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.drawable.Drawable; |
| 7 | +import android.view.View; |
| 8 | +import android.widget.Button; |
| 9 | +import android.widget.ImageView; |
| 10 | +import android.widget.RelativeLayout; |
| 11 | +import android.widget.TextView; |
| 12 | + |
| 13 | +/** |
| 14 | + * Created by Yoav. |
| 15 | + */ |
| 16 | +public class QCircleDialog { |
| 17 | + Activity activity; |
| 18 | + QCircleTemplate activityTemplate; |
| 19 | + // Dialog properties |
| 20 | + String title; |
| 21 | + String text; |
| 22 | + String positiveButtonText; |
| 23 | + String negativeButtonText; |
| 24 | + Drawable image; |
| 25 | + View.OnClickListener positiveButtonListener; |
| 26 | + View.OnClickListener negativeButtonListener; |
| 27 | + DialogMode mode; |
| 28 | + View templateLayout; |
| 29 | + |
| 30 | + private QCircleDialog(Builder builder) { |
| 31 | + this.title = builder.title; |
| 32 | + this.text = builder.text; |
| 33 | + this.positiveButtonText = builder.positiveButtonText; |
| 34 | + this.negativeButtonText = builder.negativeButtonText; |
| 35 | + this.image = builder.image; |
| 36 | + this.positiveButtonListener = builder.positiveButtonListener; |
| 37 | + this.negativeButtonListener = builder.negativeButtonListener; |
| 38 | + this.mode = builder.mode; |
| 39 | + } |
| 40 | + |
| 41 | + public static class Builder { |
| 42 | + private String title = null; |
| 43 | + private String text; |
| 44 | + private String positiveButtonText; |
| 45 | + private String negativeButtonText; |
| 46 | + private Drawable image = null; |
| 47 | + private View.OnClickListener positiveButtonListener; |
| 48 | + private View.OnClickListener negativeButtonListener = null; |
| 49 | + private QCircleDialog.DialogMode mode = DialogMode.Ok; |
| 50 | + |
| 51 | + public Builder setTitle(String title) { |
| 52 | + this.title = title; |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + public Builder setText(String text) { |
| 57 | + this.text = text; |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + public Builder setImage(Drawable image) { |
| 62 | + this.image = image; |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + public Builder setPositiveButtonListener(View.OnClickListener positiveButtonListener) { |
| 67 | + this.positiveButtonListener = positiveButtonListener; |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + public Builder setNegativeButtonListener(View.OnClickListener negativeButtonListener) { |
| 72 | + this.negativeButtonListener = negativeButtonListener; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public Builder setMode(QCircleDialog.DialogMode mode) { |
| 77 | + this.mode = mode; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public void setPositiveButtonText(String positiveButtonText) { |
| 82 | + this.positiveButtonText = positiveButtonText; |
| 83 | + } |
| 84 | + |
| 85 | + public void setNegativeButtonText(String negativeButtonText) { |
| 86 | + this.negativeButtonText = negativeButtonText; |
| 87 | + } |
| 88 | + |
| 89 | + public QCircleDialog create() { |
| 90 | + return new QCircleDialog(this); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Show the dialog |
| 96 | + * |
| 97 | + * @param activity The activity |
| 98 | + * @param activityTemplate The template that the activity uses |
| 99 | + */ |
| 100 | + public void show(Activity activity, QCircleTemplate activityTemplate) { |
| 101 | + this.activity = activity; |
| 102 | + this.activityTemplate = activityTemplate; |
| 103 | + RelativeLayout layout = (RelativeLayout) activityTemplate.getLayoutById(TemplateTag.CONTENT).getParent(); |
| 104 | + QCircleTemplate template = new QCircleTemplate(activity); |
| 105 | + template.setTitle(title == null ? "" : title, Color.WHITE, activity.getResources().getColor( |
| 106 | + mode == DialogMode.Error ? R.color.dialog_title_background_color_error : R.color.dialog_title_background_color_regular)); |
| 107 | + template.setTitleTextSize(17); |
| 108 | + RelativeLayout dialogLayout = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.qcircle_dialog_layout, layout, false); |
| 109 | + if (text != null) { |
| 110 | + ((TextView) dialogLayout.findViewById(R.id.text)).setText(text); |
| 111 | + } |
| 112 | + if (image != null) { |
| 113 | + ((ImageView) dialogLayout.findViewById(R.id.image)).setImageDrawable(image); |
| 114 | + } |
| 115 | + switch (mode) { |
| 116 | + case YesNo: |
| 117 | + Button negativeButton = (Button) dialogLayout.findViewById(R.id.negative); |
| 118 | + if (negativeButtonText != null) { |
| 119 | + negativeButton.setText(negativeButtonText); |
| 120 | + } |
| 121 | + negativeButton.setOnClickListener(new View.OnClickListener() { |
| 122 | + @Override |
| 123 | + public void onClick(View v) { |
| 124 | + if (negativeButtonListener != null) negativeButtonListener.onClick(v); |
| 125 | + hide(); |
| 126 | + } |
| 127 | + }); |
| 128 | + case Ok: |
| 129 | + Button positiveButton = (Button) dialogLayout.findViewById(R.id.positive); |
| 130 | + if (positiveButtonText != null) { |
| 131 | + positiveButton.setText(positiveButtonText); |
| 132 | + } |
| 133 | + positiveButton.setOnClickListener(new View.OnClickListener() { |
| 134 | + @Override |
| 135 | + public void onClick(View v) { |
| 136 | + if (positiveButtonListener != null) positiveButtonListener.onClick(v); |
| 137 | + hide(); |
| 138 | + } |
| 139 | + }); |
| 140 | + break; |
| 141 | + case Error: |
| 142 | + @SuppressLint("CutPasteId") |
| 143 | + Button errorButton = (Button) dialogLayout.findViewById(R.id.positive); |
| 144 | + if (negativeButtonText != null) { |
| 145 | + errorButton.setText(negativeButtonText); |
| 146 | + } |
| 147 | + errorButton.setOnClickListener(new View.OnClickListener() { |
| 148 | + @Override |
| 149 | + public void onClick(View v) { |
| 150 | + if (negativeButtonListener != null) negativeButtonListener.onClick(v); |
| 151 | + hide(); |
| 152 | + } |
| 153 | + }); |
| 154 | + break; |
| 155 | + |
| 156 | + } |
| 157 | + if (mode != DialogMode.YesNo) |
| 158 | + dialogLayout.findViewById(R.id.negative).setVisibility(View.GONE); |
| 159 | + template.getLayoutById(TemplateTag.CONTENT).addView(dialogLayout); |
| 160 | + layout.addView(templateLayout = template.getView()); |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + public void hide() { |
| 165 | + ((RelativeLayout) activityTemplate.getLayoutById(TemplateTag.CONTENT).getParent()).removeView(templateLayout); |
| 166 | + templateLayout = null; |
| 167 | + } |
| 168 | + |
| 169 | + public enum DialogMode { |
| 170 | + /** |
| 171 | + * A dialog with yes and no buttons. |
| 172 | + */ |
| 173 | + YesNo, |
| 174 | + /** |
| 175 | + * A dialog with ok button only. |
| 176 | + */ |
| 177 | + Ok, |
| 178 | + /** |
| 179 | + * An error dialog. Shows only back button, which finish the activity. |
| 180 | + */ |
| 181 | + Error |
| 182 | + } |
| 183 | +} |
0 commit comments