Kotlin / anko

Pleasant Android application development
Apache License 2.0
15.88k stars 1.29k forks source link

Dismissing Anko Dialog in onClickListener #685

Open qbait opened 5 years ago

qbait commented 5 years ago

I would like to dismiss Anko Dialog when pressing the button defined in my customView

val dialog = alert {
   val view = layoutInflater.inflate(R.layout.match_stats, null)
   val closeButton = view.findViewById<ImageButton>(R.id.closeButton)
   closeButton.setOnClickListener { _ -> dialog.dismiss()}
   customView = view
}
dialog.show()

I tried above code, unfortunately, I can’t get a reference to dialog in my onClickListener. Do you have any idea how to solve it?

mrhammadasif commented 5 years ago

You would get the reference of dialog in it

iceboundrock commented 5 years ago

it is impossible since the type of dialog is AlertBuilder<DialogInterface> which does not have a dismiss method.

I have an ulgry solution like this:

var dialog: DialogInterface? = null
dialog = alert {
    val view = layoutInflater.inflate(R.layout.match_stats, null)
    val closeButton = view.findViewById<ImageButton>(R.id.closeButton)

    closeButton.setOnClickListener { _ -> dialog?.dismiss() }
    customView = view
}.show()