Advertisement
DontPanic284

Untitled

May 1st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.14 KB | None | 0 0
  1. class CustomDialog(context: Context, val message: CharSequence,
  2.                    private val buttonText: CharSequence = context.getString(R.string.button_dismiss),
  3.                    private val title: CharSequence = "",
  4.                    private val textGravity: Int = Gravity.CENTER,
  5.                    private val typeDialog: TypeDialog = NORMAL)
  6.     : AlertDialog(context), View.OnClickListener {
  7.  
  8.     constructor(context: Context, message: CharSequence,
  9.                 positiveButtonText: CharSequence = context.getString(R.string.button_yes),
  10.                 negativeButtonText: CharSequence = context.getString(R.string.button_dismiss),
  11.                 onPositiveClickListener: View.OnClickListener?) : this(context, message, negativeButtonText) {
  12.         this.onPositiveClickListener = onPositiveClickListener
  13.         this.positiveButtonText = positiveButtonText
  14.     }
  15.  
  16.     constructor(context: Context, message: CharSequence, negativeButtonText: CharSequence,
  17.                 onNegativeClickListener: View.OnClickListener?) : this(context, message, negativeButtonText) {
  18.         this.onNegativeClickListener = onNegativeClickListener
  19.     }
  20.  
  21.     private var onNegativeClickListener: View.OnClickListener? = null
  22.     private var onPositiveClickListener: View.OnClickListener? = null
  23.     private var positiveButtonText: CharSequence? = null
  24.  
  25.     enum class TypeDialog {
  26.         NORMAL, FULL
  27.     }
  28.  
  29.     override fun onCreate(savedInstanceState: Bundle?) {
  30.         requestWindowFeature(Window.FEATURE_NO_TITLE)
  31.         super.onCreate(savedInstanceState)
  32.  
  33.         setContentView(R.layout.dialog_custom)
  34.         setCanceledOnTouchOutside(false)
  35.         window?.let {
  36.             it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
  37.             it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
  38.                     ViewGroup.LayoutParams.WRAP_CONTENT)
  39.         }
  40.         when (typeDialog) {
  41.             TypeDialog.FULL -> ivIcon.visibility = View.VISIBLE
  42.             else -> ivIcon.visibility = View.GONE
  43.         }
  44.  
  45.         if (title != "") {
  46.             tvTitle.visibility = View.VISIBLE
  47.             tvTitle.text = title
  48.         }
  49.  
  50.         tvMessage.text = message
  51.         tvMessage.gravity = textGravity
  52.  
  53.         btnOk.apply {
  54.             positiveButtonText?.let { text = it.toString().toUpperCase(Locale.getDefault()) }
  55.  
  56.             visibility = if (positiveButtonText == null) View.GONE else View.VISIBLE
  57.             setTextColor(StoreManager.getColorSecondaryResId(context))
  58.             setOnClickAction(this@CustomDialog)
  59.         }
  60.  
  61.         btnDismiss.apply {
  62.             text = buttonText.toString().toUpperCase(Locale.getDefault())
  63.             setTextColor(StoreManager.getColorSecondaryResId(context))
  64.             setOnClickAction(this@CustomDialog)
  65.         }
  66.     }
  67.  
  68.     override fun onClick(v: View?) {
  69.         when (v?.id) {
  70.             btnDismiss.id -> {
  71.                 onNegativeClickListener?.onClick(v)
  72.                 if (isShowing) dismiss()
  73.             }
  74.             btnOk.id -> {
  75.                 onPositiveClickListener?.onClick(v)
  76.                 if (isShowing) dismiss()
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement