Search code examples
androiddialogretrofit2response

How to use a custom Dialog in kotlin


I have this small class that i want use to show and close my dialog, my retrofit reponse is loading the list of items

class ShowProgress(context: Context) : Dialog(context) {

var dialog = Dialog(context)

fun showDialog() {
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(false)
    dialog.setContentView(R.layout.progress_layout)
    dialog.show()
}

fun closeDialog() {
    dialog.dismiss()
}
}

Im getting the dialog to show up but it wont dismiss() when the response is complete. What whould be the issue here exactly?


Solution

  • This how you can show and dismiss custom popup;

    
    class ShowProgress(context: Context) : Dialog(context) {
    
        init {
            dialog = Dialog(context)
        }
    
        fun showPopup() {
    
            val dialogview = LayoutInflater.from(context)
                .inflate(R.layout.dialog_choose_image, null, false)
            //initializing dialog screen
    
            dialog?.setCancelable(true)
            dialog?.setContentView(dialogview)
            dialog?.show()
    
        }
        companion object{
            var dialog: Dialog? = null
            fun dismissPopup() = dialog?.let { dialog!!.dismiss() }
        }
    
    }
    

    and from view you can access it like this. val showProgress = ShowProgress(this) showProgress.showPopup()

    and dismiss it whenever you want to dismiss popup ShowProgress.dismissPopup()