How to create a Custom Dialog box in android?

后端 未结 22 2839
囚心锁ツ
囚心锁ツ 2020-11-21 07:06

I want to create a custom dialog box like below

\"enter

I have tried the foll

22条回答
  •  失恋的感觉
    2020-11-21 07:38

    I am posting the kotlin code that I am using and it works fine for me. you can also set click listener for dialog buttons.

    this is my XML code:

    layout_custom_alert_dialog.xml

    
    
    
        
    
        
    
    
            
    
    
            

    mAlertDialog.kt

    class mAlertDialog(context: Context) {
    
        private val btn_positive : Button
        private val btn_negative : Button
        private val txt_alert_title : TextView
        private val dialog : AlertDialog
        init {
            val view = LayoutInflater.from(context).inflate(R.layout.layout_custom_alert_dialog,null)
    
            val dialog_builder = AlertDialog.Builder(context)
            dialog_builder.setView(view)
    
            btn_negative = view.findViewById(R.id.btn_alert_negative)
            btn_positive = view.findViewById(R.id.btn_alert_positive)
            txt_alert_title = view.findViewById(R.id.txt_alert_title)
    
            dialog = dialog_builder.create() 
        }
    
        fun show()
        {
            dialog.show()
        }
    
        fun setPositiveClickListener(listener :onClickListener)
        {
            btn_positive.setOnClickListener { v ->
                listener.onClick(btn_positive)
                dialog.dismiss()
            }
        }
    
        fun setNegativeClickListener(listener: onClickListener)
        {
            btn_negative.setOnClickListener { v ->
                listener.onClick(btn_negative)
                dialog.dismiss()
            }
        }
    
        fun setPoitiveButtonText(text : String)
        {
            btn_positive.text = text
        }
    
    
        fun setNegativeButtonText(text : String)
        {
            btn_negative.text = text
        }
    
        fun setAlertTitle(title : String)
        {
            txt_alert_title.text = title
        }
    }
    

    interface for click listeners:

    onClickListener.kt

    interface onClickListener{
        fun onClick(view : View)
    }
    

    Sample Usage

    val dialog = mAlertDialog(context)
                    dialog.setNegativeButtonText("no i dont")
                    dialog.setPoitiveButtonText("yes is do")
                    dialog.setAlertTitle("do you like this alert dialog?")
    
                    dialog.setPositiveClickListener(object : onClickListener {
                        override fun onClick(view: View) {
                            Toast.makeText(context, "yes", Toast.LENGTH_SHORT).show()
                        }
                    })
    
                    dialog.setNegativeClickListener(object : onClickListener {
                        override fun onClick(view: View) {
                            Toast.makeText(context, "no", Toast.LENGTH_SHORT).show()
                        }
                    })
    
                    dialog.show()
    

    I hope, this will help you!

提交回复
热议问题