Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CustomDialog(context: Context, val message: CharSequence,
- private val buttonText: CharSequence = context.getString(R.string.button_dismiss),
- private val title: CharSequence = "",
- private val textGravity: Int = Gravity.CENTER,
- private val typeDialog: TypeDialog = NORMAL)
- : AlertDialog(context), View.OnClickListener {
- constructor(context: Context, message: CharSequence,
- positiveButtonText: CharSequence = context.getString(R.string.button_yes),
- negativeButtonText: CharSequence = context.getString(R.string.button_dismiss),
- onPositiveClickListener: View.OnClickListener?) : this(context, message, negativeButtonText) {
- this.onPositiveClickListener = onPositiveClickListener
- this.positiveButtonText = positiveButtonText
- }
- constructor(context: Context, message: CharSequence, negativeButtonText: CharSequence,
- onNegativeClickListener: View.OnClickListener?) : this(context, message, negativeButtonText) {
- this.onNegativeClickListener = onNegativeClickListener
- }
- private var onNegativeClickListener: View.OnClickListener? = null
- private var onPositiveClickListener: View.OnClickListener? = null
- private var positiveButtonText: CharSequence? = null
- enum class TypeDialog {
- NORMAL, FULL
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- requestWindowFeature(Window.FEATURE_NO_TITLE)
- super.onCreate(savedInstanceState)
- setContentView(R.layout.dialog_custom)
- setCanceledOnTouchOutside(false)
- window?.let {
- it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
- it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
- ViewGroup.LayoutParams.WRAP_CONTENT)
- }
- when (typeDialog) {
- TypeDialog.FULL -> ivIcon.visibility = View.VISIBLE
- else -> ivIcon.visibility = View.GONE
- }
- if (title != "") {
- tvTitle.visibility = View.VISIBLE
- tvTitle.text = title
- }
- tvMessage.text = message
- tvMessage.gravity = textGravity
- btnOk.apply {
- positiveButtonText?.let { text = it.toString().toUpperCase(Locale.getDefault()) }
- visibility = if (positiveButtonText == null) View.GONE else View.VISIBLE
- setTextColor(StoreManager.getColorSecondaryResId(context))
- setOnClickAction(this@CustomDialog)
- }
- btnDismiss.apply {
- text = buttonText.toString().toUpperCase(Locale.getDefault())
- setTextColor(StoreManager.getColorSecondaryResId(context))
- setOnClickAction(this@CustomDialog)
- }
- }
- override fun onClick(v: View?) {
- when (v?.id) {
- btnDismiss.id -> {
- onNegativeClickListener?.onClick(v)
- if (isShowing) dismiss()
- }
- btnOk.id -> {
- onPositiveClickListener?.onClick(v)
- if (isShowing) dismiss()
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement