Advertisement

Responsive Advertisement

Fancy Dialog for AlertDialog in Android Studio


<style name="PopTheme" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
<item name="android:windowAnimationStyle">@style/PopTheme1</item>
</style>

<style name="PopTheme1">
<item name="android:windowEnterAnimation">@anim/pop_in</item>
<item name="android:windowExitAnimation">@anim/pop_out</item>
</style>

<style name="SideTheme" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
<item name="android:windowAnimationStyle">@style/SideTheme1</item>
</style>

<style name="SideTheme1">
<item name="android:windowEnterAnimation">@anim/side_in</item>
<item name="android:windowExitAnimation">@anim/side_out</item>
</style>

<style name="SlideTheme" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
<item name="android:windowAnimationStyle">@style/SlideTheme1</item>
</style>

<style name="SlideTheme1">
<item name="android:windowEnterAnimation">@anim/slide_in</item>
<item name="android:windowExitAnimation">@anim/slide_out</item>
</style>

  • For kotlin create this class and paste this code
    import android.app.Dialog
    import android.content.Context
    import android.graphics.Color
    import android.graphics.drawable.ColorDrawable
    import android.graphics.drawable.GradientDrawable
    import android.view.View
    import android.view.Window
    import android.widget.Button
    import android.widget.ImageView
    import android.widget.TextView
    import androidx.annotation.ColorInt
    import androidx.annotation.ColorRes
    import androidx.annotation.DrawableRes
    import com.example.test.R
    import java.lang.ref.WeakReference

    object FancyAlertDialog1{

    class Builder(context: Context) {

    private var title: String? = null
    private var message: String? = null
    private var positiveBtnText: String? = null
    private var negativeBtnText: String? = null

    @DrawableRes
    private var icon: Int = 0
    private var visibility: Int = View.VISIBLE
    private var animation: Animation = Animation.NONE
    private var pListener: FancyAlertDialogListener? = null
    private var nListener: FancyAlertDialogListener? = null

    @ColorInt
    private var pBtnColor: Int = 0

    @ColorInt
    private var nBtnColor: Int = 0

    @ColorInt
    private var bgColor: Int = 0
    private var cancel: Boolean = true
    private var dialog: Dialog
    private val contextRef: WeakReference<Context> = WeakReference(context)

    init {
    dialog = Dialog(context)
    }

    fun setTitle(title: String) = apply { this.title = title }
    fun setBackgroundColor(@ColorInt bgColor: Int) = apply { this.bgColor = bgColor }
    fun setBackgroundColorRes(@ColorRes bgColor: Int) =
    apply { this.bgColor = contextRef.get()?.resources?.getColor(bgColor) ?: 0 }

    fun setMessage(message: String) = apply { this.message = message }
    fun setPositiveBtnText(positiveBtnText: String) =
    apply { this.positiveBtnText = positiveBtnText }

    fun setPositiveBtnBackground(@ColorInt pBtnColor: Int) =
    apply { this.pBtnColor = pBtnColor }

    fun setPositiveBtnBackgroundRes(@ColorRes pBtnColor: Int) =
    apply { this.pBtnColor = contextRef.get()?.resources?.getColor(pBtnColor) ?: 0 }

    fun setNegativeBtnText(negativeBtnText: String) =
    apply { this.negativeBtnText = negativeBtnText }

    fun setNegativeBtnBackground(@ColorInt nBtnColor: Int) =
    apply { this.nBtnColor = nBtnColor }

    fun setNegativeBtnBackgroundRes(@ColorRes nBtnColor: Int) =
    apply { this.nBtnColor = contextRef.get()?.resources?.getColor(nBtnColor) ?: 0 }

    fun setIcon(@DrawableRes icon: Int, visibility: Int) = apply {
    this.icon = icon
    this.visibility = visibility
    }

    fun setAnimation(animation: Animation) = apply { this.animation = animation }
    fun onPositiveClicked(pListener: FancyAlertDialogListener?) =
    apply { this.pListener = pListener }

    fun onNegativeClicked(nListener: FancyAlertDialogListener?) =
    apply { this.nListener = nListener }

    fun isCancellable(cancel: Boolean) = apply { this.cancel = cancel }

    fun build(): FancyAlertDialog1 {
    val view: View
    val titleView: TextView
    val messageView: TextView
    val iconImg: ImageView
    val nBtn: Button
    val pBtn: Button

    dialog = when (animation) {
    Animation.POP -> Dialog(contextRef.get()!!, R.style.PopTheme)
    Animation.SIDE -> Dialog(contextRef.get()!!, R.style.SideTheme)
    Animation.SLIDE -> Dialog(contextRef.get()!!, R.style.SlideTheme)
    else -> Dialog(contextRef.get()!!)
    }

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    dialog.setCancelable(cancel)
    dialog.setContentView(R.layout.fancyalertdialog)

    // getting resources
    view = dialog.findViewById(R.id.background)
    titleView = dialog.findViewById(R.id.title)
    messageView = dialog.findViewById(R.id.message)
    iconImg = dialog.findViewById(R.id.icon)
    nBtn = dialog.findViewById(R.id.negativeBtn)
    pBtn = dialog.findViewById(R.id.positiveBtn)

    titleView.text = title
    messageView.text = message
    positiveBtnText?.let { pBtn.text = it }
    if (pBtnColor != 0) {
    val bgShape = pBtn.background as GradientDrawable
    bgShape.setColor(pBtnColor)
    }
    if (nBtnColor != 0) {
    val bgShape = nBtn.background as GradientDrawable
    bgShape.setColor(nBtnColor)
    }
    negativeBtnText?.let { nBtn.text = it }
    iconImg.setImageResource(icon)
    iconImg.visibility = visibility
    if (bgColor != 0) view.setBackgroundColor(bgColor)

    pListener?.let {
    pBtn.setOnClickListener {
    pListener?.onClick(dialog)
    dialog.dismiss()
    }
    } ?: run {
    pBtn.setOnClickListener { dialog.dismiss() }
    }

    nListener?.let {
    nBtn.visibility = View.VISIBLE
    nBtn.setOnClickListener {
    nListener?.onClick(dialog)
    dialog.dismiss()
    }
    }
    dialog.show()

    return FancyAlertDialog1
    }
    }
    }

    interface FancyAlertDialogListener {
    fun onClick(dialog: Dialog)
    }

    enum class Animation {
    NONE, POP, SIDE, SLIDE
    }
  • Now paste this on mainactivity file or fragment
    FancyAlertDialog1.Builder(this)
    .setTitle("Alert Title")
    .setMessage("This is a fancy alert dialog message.")
    .setPositiveBtnText("OK")
    .setNegativeBtnText("Cancel")
    .setIcon(
    R.drawable.ic_check_black_24dp,
    View.VISIBLE
    ) // Assuming you have this drawable
    .setAnimation(Animation.POP)
    .setBackgroundColorRes(R.color.black) // Assuming you have this color resource
    .setPositiveBtnBackgroundRes(R.color.black) // Assuming you have this color resource
    .setNegativeBtnBackgroundRes(R.color.white) // Assuming you have this color resource
    .onPositiveClicked(object : FancyAlertDialogListener {
    override fun onClick(dialog: Dialog) {
    // Handle positive button click
    }
    })
    .onNegativeClicked(object : FancyAlertDialogListener {
    override fun onClick(dialog: Dialog) {
    // Handle negative button click
    }
    })
    .isCancellable(true)
    .build()
  • If you're using Java, then
    Add this 
    public enum Animation {
    POP, SIDE, SLIDE
    }
  • Then add this,
    public interface FancyAlertDialogListener {
    void onClick(Dialog dialog);
    }
  • Then add this
    @IntDef({VISIBLE, INVISIBLE, GONE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Visibility {
    }
  • Then add this,
    public class FancyAlertDialog {

    private String title, message, positiveBtnText, negativeBtnText;
    @DrawableRes
    private int icon;
    @Visibility
    private int visibility;
    private Animation animation;
    private FancyAlertDialogListener pListener, nListener;
    @ColorInt
    private int pBtnColor, nBtnColor, bgColor;
    private boolean cancel;
    private Dialog dialog;

    private FancyAlertDialog(Builder builder) {
    this.title = builder.title;
    this.message = builder.message;
    this.icon = builder.icon;
    this.animation = builder.animation;
    this.visibility = builder.visibility;
    this.pListener = builder.pListener;
    this.nListener = builder.nListener;
    this.positiveBtnText = builder.positiveBtnText;
    this.negativeBtnText = builder.negativeBtnText;
    this.pBtnColor = builder.pBtnColor;
    this.nBtnColor = builder.nBtnColor;
    this.bgColor = builder.bgColor;
    this.cancel = builder.cancel;
    this.dialog = builder.dialog;
    }

    public FancyAlertDialog show() {
    dialog.show();
    return this;
    }

    public FancyAlertDialog dismiss() {
    dialog.dismiss();
    return this;
    }

    // region builder

    public static class Builder {

    private String title, message, positiveBtnText, negativeBtnText;
    @DrawableRes
    private int icon;
    @Visibility
    private int visibility;
    private Animation animation;
    private FancyAlertDialogListener pListener, nListener;
    @ColorInt
    private int pBtnColor, nBtnColor, bgColor;
    private boolean cancel;

    /**
    * Always have weak references of context in libraries to avoid memory leaks.
    **/
    private WeakReference<Context> context;
    private Dialog dialog;

    private Builder(Context context) {
    // use #with
    this.context = new WeakReference<>(context);
    }

    public static Builder with(@NonNull Context context) {
    return new Builder(context);
    }

    public Builder setTitle(String title) {
    this.title = title;
    return this;
    }

    public Builder setBackgroundColor(@ColorInt int bgColor) {
    this.bgColor = bgColor;
    return this;
    }

    public Builder setBackgroundColorRes(@ColorRes int bgColor) {
    return setBackgroundColor(context.get().getResources().getColor(bgColor));
    }

    public Builder setMessage(String message) {
    this.message = message;
    return this;
    }

    public Builder setPositiveBtnText(String positiveBtnText) {
    this.positiveBtnText = positiveBtnText;
    return this;
    }

    public Builder setPositiveBtnBackground(@ColorInt int pBtnColor) {
    this.pBtnColor = pBtnColor;
    return this;
    }

    public Builder setPositiveBtnBackgroundRes(@ColorRes int pBtnColor) {
    return setPositiveBtnBackground(context.get().getResources().getColor(pBtnColor));
    }

    public Builder setNegativeBtnText(String negativeBtnText) {
    this.negativeBtnText = negativeBtnText;
    return this;
    }

    public Builder setNegativeBtnBackground(@ColorInt int nBtnColor) {
    this.nBtnColor = nBtnColor;
    return this;
    }

    public Builder setNegativeBtnBackgroundRes(@ColorRes int nBtnColor) {
    return setNegativeBtnBackground(context.get().getResources().getColor(nBtnColor));
    }

    //setIcon
    public Builder setIcon(@DrawableRes int icon, @Visibility int visibility) {
    this.icon = icon;
    this.visibility = visibility;
    return this;
    }

    public Builder setAnimation(Animation animation) {
    this.animation = animation;
    return this;
    }

    //set Positive listener
    public Builder onPositiveClicked(@Nullable FancyAlertDialogListener pListener) {
    this.pListener = pListener;
    return this;
    }

    //set Negative listener
    public Builder onNegativeClicked(@Nullable FancyAlertDialogListener nListener) {
    this.nListener = nListener;
    return this;
    }

    public Builder isCancellable(boolean cancel) {
    this.cancel = cancel;
    return this;
    }

    public FancyAlertDialog build() {
    TextView message1, title1;
    ImageView iconImg;
    Button nBtn, pBtn;
    View view;
    if (animation == Animation.POP)
    dialog = new Dialog(context.get(), R.style.PopTheme);
    else if (animation == Animation.SIDE)
    dialog = new Dialog(context.get(), R.style.SideTheme);
    else if (animation == Animation.SLIDE)
    dialog = new Dialog(context.get(), R.style.SlideTheme);
    else
    dialog = new Dialog(context.get());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.setCancelable(cancel);
    dialog.setContentView(R.layout.fancyalertdialog);

    //getting resources
    view = dialog.findViewById(R.id.background);
    title1 = dialog.findViewById(R.id.title);
    message1 = dialog.findViewById(R.id.message);
    iconImg = dialog.findViewById(R.id.icon);
    nBtn = dialog.findViewById(R.id.negativeBtn);
    pBtn = dialog.findViewById(R.id.positiveBtn);
    title1.setText(title);
    message1.setText(message);
    if (positiveBtnText != null)
    pBtn.setText(positiveBtnText);
    if (pBtnColor != 0) {
    GradientDrawable bgShape = (GradientDrawable) pBtn.getBackground();
    bgShape.setColor(pBtnColor);
    }
    if (nBtnColor != 0) {
    GradientDrawable bgShape = (GradientDrawable) nBtn.getBackground();
    bgShape.setColor(nBtnColor);
    }
    if (negativeBtnText != null)
    nBtn.setText(negativeBtnText);
    iconImg.setImageResource(icon);
    iconImg.setVisibility(visibility);
    if (bgColor != 0)
    view.setBackgroundColor(bgColor);
    if (pListener != null) {
    pBtn.setOnClickListener(v -> {
    pListener.onClick(dialog);
    dialog.dismiss();
    });
    } else {
    pBtn.setOnClickListener(v -> dialog.dismiss());
    }

    if (nListener != null) {
    nBtn.setVisibility(View.VISIBLE);
    nBtn.setOnClickListener(v -> {
    nListener.onClick(dialog);
    dialog.dismiss();
    });
    }

    // note: builder should only create an object and
    // leave the showing/dismissing of the dialog to library user
    // dialog.show();

    return new FancyAlertDialog(this);

    }
    }

    // endregion

    }
  • Then add this in Main Activity
    FancyAlertDialog.Builder
    .with(this)
    .setTitle("Rate us if you like the app")
    .setBackgroundColor(Color.parseColor("#303F9F")) // for @ColorRes use setBackgroundColorRes(R.color.colorvalue)
    .setMessage("Do you really want to Exit ?")
    .setNegativeBtnText("Cancel")
    .setPositiveBtnBackground(Color.parseColor("#FF4081")) // for @ColorRes use setPositiveBtnBackgroundRes(R.color.colorvalue)
    .setPositiveBtnText("Rate")
    .setNegativeBtnBackground(Color.parseColor("#FFA9A7A8")) // for @ColorRes use setNegativeBtnBackgroundRes(R.color.colorvalue)
    .setAnimation(Animation.POP)
    .isCancellable(true)
    .setIcon(R.drawable.ic_check_black_24dp, View.VISIBLE)
    .onPositiveClicked { dialog: Dialog? ->
    Toast.makeText(
    this@MainActivity,
    "Rate",
    Toast.LENGTH_SHORT
    ).show()
    }
    .onNegativeClicked { dialog: Dialog? ->
    Toast.makeText(
    this@MainActivity,
    "Cancel",
    Toast.LENGTH_SHORT
    ).show()
    }
    .build()
    .show()

Post a Comment

0 Comments