|
| 1 | +package com.amit.dialog; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.Dialog; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.drawable.ColorDrawable; |
| 7 | +import android.graphics.drawable.GradientDrawable; |
| 8 | +import android.view.View; |
| 9 | +import android.view.Window; |
| 10 | +import android.widget.Button; |
| 11 | +import android.widget.ImageView; |
| 12 | +import android.widget.TextView; |
| 13 | + |
| 14 | +import com.amit.R; |
| 15 | + |
| 16 | +import static com.amit.dialog.Anim.POP; |
| 17 | +import static com.amit.dialog.Anim.SIDE; |
| 18 | +import static com.amit.dialog.Anim.SLIDE; |
| 19 | + |
| 20 | +/** |
| 21 | + * Created by Amit Jangid on 21,May,2018 |
| 22 | + **/ |
| 23 | +@SuppressWarnings({"WeakerAcces", "unused"}) |
| 24 | +public class AlertDialogBox |
| 25 | +{ |
| 26 | + private String title, message, positiveBtnText, negativeBtnText; |
| 27 | + private Activity activity; |
| 28 | + private int icon; |
| 29 | + private Icon visibility; |
| 30 | + private com.amit.dialog.Anim Anim; |
| 31 | + private AlertDialogListener pListener, nListener; |
| 32 | + private int pBtnColor, nBtnColor, bgColor; |
| 33 | + private boolean cancel; |
| 34 | + |
| 35 | + private AlertDialogBox(Builder builder) |
| 36 | + { |
| 37 | + this.title = builder.title; |
| 38 | + this.message = builder.message; |
| 39 | + this.activity = builder.activity; |
| 40 | + this.icon = builder.icon; |
| 41 | + this.Anim = builder.Anim; |
| 42 | + this.visibility = builder.visibility; |
| 43 | + this.pListener = builder.pListener; |
| 44 | + this.nListener = builder.nListener; |
| 45 | + this.positiveBtnText = builder.positiveBtnText; |
| 46 | + this.negativeBtnText = builder.negativeBtnText; |
| 47 | + this.pBtnColor = builder.pBtnColor; |
| 48 | + this.nBtnColor = builder.nBtnColor; |
| 49 | + this.bgColor = builder.bgColor; |
| 50 | + this.cancel = builder.cancel; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + public static class Builder |
| 55 | + { |
| 56 | + private String title, message, positiveBtnText, negativeBtnText; |
| 57 | + private Activity activity; |
| 58 | + private int icon; |
| 59 | + private Icon visibility; |
| 60 | + private com.amit.dialog.Anim Anim; |
| 61 | + private AlertDialogListener pListener, nListener; |
| 62 | + private int pBtnColor, nBtnColor, bgColor; |
| 63 | + private boolean cancel; |
| 64 | + |
| 65 | + public Builder(Activity activity) |
| 66 | + { |
| 67 | + this.activity = activity; |
| 68 | + } |
| 69 | + |
| 70 | + public Builder setTitle(String title) |
| 71 | + { |
| 72 | + this.title = title; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public Builder setBackgroundColor(int bgColor) |
| 77 | + { |
| 78 | + this.bgColor = bgColor; |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + public Builder setMessage(String message) |
| 83 | + { |
| 84 | + this.message = message; |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + public Builder setPositiveBtnText(String positiveBtnText) |
| 89 | + { |
| 90 | + this.positiveBtnText = positiveBtnText; |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + public Builder setPositiveBtnBackground(int pBtnColor) |
| 95 | + { |
| 96 | + this.pBtnColor = pBtnColor; |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + public Builder setNegativeBtnText(String negativeBtnText) |
| 101 | + { |
| 102 | + this.negativeBtnText = negativeBtnText; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + public Builder setNegativeBtnBackground(int nBtnColor) |
| 107 | + { |
| 108 | + this.nBtnColor = nBtnColor; |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + //setIcon |
| 113 | + public Builder setIcon(int icon, Icon visibility) |
| 114 | + { |
| 115 | + this.icon = icon; |
| 116 | + this.visibility = visibility; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public Builder setAnim(com.amit.dialog.Anim Anim) |
| 121 | + { |
| 122 | + this.Anim = Anim; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + //set Positive listener |
| 127 | + public Builder onPositiveClicked(AlertDialogListener pListener) |
| 128 | + { |
| 129 | + this.pListener = pListener; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + //set Negative listener |
| 134 | + public Builder onNegativeClicked(AlertDialogListener nListener) |
| 135 | + { |
| 136 | + this.nListener = nListener; |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + public Builder isCancellable(boolean cancel) |
| 141 | + { |
| 142 | + this.cancel = cancel; |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + public AlertDialogBox build() |
| 147 | + { |
| 148 | + TextView message1, title1; |
| 149 | + ImageView iconImg; |
| 150 | + Button nBtn, pBtn; |
| 151 | + View view; |
| 152 | + |
| 153 | + final Dialog dialog; |
| 154 | + |
| 155 | + if (Anim == POP) |
| 156 | + { |
| 157 | + dialog = new Dialog(activity, R.style.PopTheme); |
| 158 | + } |
| 159 | + else if (Anim == SIDE) |
| 160 | + { |
| 161 | + dialog = new Dialog(activity, R.style.SideTheme); |
| 162 | + } |
| 163 | + else if (Anim == SLIDE) |
| 164 | + { |
| 165 | + dialog = new Dialog(activity, R.style.SlideTheme); |
| 166 | + } |
| 167 | + else |
| 168 | + { |
| 169 | + dialog = new Dialog(activity); |
| 170 | + } |
| 171 | + |
| 172 | + dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 173 | + dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
| 174 | + dialog.setCancelable(cancel); |
| 175 | + dialog.setContentView(R.layout.custom_alert_dialog); |
| 176 | + |
| 177 | + //getting resources |
| 178 | + view = dialog.findViewById(R.id.background); |
| 179 | + title1 = dialog.findViewById(R.id.title); |
| 180 | + message1 = dialog.findViewById(R.id.message); |
| 181 | + iconImg = dialog.findViewById(R.id.icon); |
| 182 | + nBtn = dialog.findViewById(R.id.negativeBtn); |
| 183 | + pBtn = dialog.findViewById(R.id.positiveBtn); |
| 184 | + title1.setText(title); |
| 185 | + message1.setText(message); |
| 186 | + |
| 187 | + if (positiveBtnText != null) |
| 188 | + { |
| 189 | + pBtn.setText(positiveBtnText); |
| 190 | + } |
| 191 | + |
| 192 | + if (pBtnColor != 0) |
| 193 | + { |
| 194 | + GradientDrawable bgShape = (GradientDrawable) pBtn.getBackground(); |
| 195 | + bgShape.setColor(pBtnColor); |
| 196 | + } |
| 197 | + |
| 198 | + if (nBtnColor != 0) |
| 199 | + { |
| 200 | + GradientDrawable bgShape = (GradientDrawable) nBtn.getBackground(); |
| 201 | + bgShape.setColor(nBtnColor); |
| 202 | + } |
| 203 | + |
| 204 | + if (negativeBtnText != null) |
| 205 | + { |
| 206 | + nBtn.setText(negativeBtnText); |
| 207 | + } |
| 208 | + |
| 209 | + iconImg.setImageResource(icon); |
| 210 | + |
| 211 | + if (visibility == Icon.Visible) |
| 212 | + { |
| 213 | + iconImg.setVisibility(View.VISIBLE); |
| 214 | + } |
| 215 | + else |
| 216 | + { |
| 217 | + iconImg.setVisibility(View.GONE); |
| 218 | + } |
| 219 | + |
| 220 | + if (bgColor != 0) |
| 221 | + { |
| 222 | + view.setBackgroundColor(bgColor); |
| 223 | + } |
| 224 | + |
| 225 | + if (pListener != null) |
| 226 | + { |
| 227 | + pBtn.setOnClickListener(new View.OnClickListener() |
| 228 | + { |
| 229 | + @Override |
| 230 | + public void onClick(View view) |
| 231 | + { |
| 232 | + pListener.onClick(); |
| 233 | + dialog.dismiss(); |
| 234 | + } |
| 235 | + }); |
| 236 | + } |
| 237 | + else |
| 238 | + { |
| 239 | + pBtn.setOnClickListener(new View.OnClickListener() |
| 240 | + { |
| 241 | + @Override |
| 242 | + public void onClick(View view) |
| 243 | + { |
| 244 | + dialog.dismiss(); |
| 245 | + } |
| 246 | + }); |
| 247 | + } |
| 248 | + |
| 249 | + if (nListener != null) |
| 250 | + { |
| 251 | + nBtn.setVisibility(View.VISIBLE); |
| 252 | + |
| 253 | + nBtn.setOnClickListener(new View.OnClickListener() |
| 254 | + { |
| 255 | + @Override |
| 256 | + public void onClick(View view) |
| 257 | + { |
| 258 | + nListener.onClick(); |
| 259 | + dialog.dismiss(); |
| 260 | + } |
| 261 | + }); |
| 262 | + } |
| 263 | + |
| 264 | + dialog.show(); |
| 265 | + return new AlertDialogBox(this); |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments