I want to make AlertDialog
with completely custom layout. If user use button it should now close AlertDialog
only if I call dialog.cancel()
.
I changed my dialog to this structure:
val builder = AlertDialog.Builder(this, R.style.AlertDialogStyle)
val inflater = this.layoutInflater
val dialogView: View = inflater.inflate(R.layout.alert_dialog_et_layout, null)
builder.apply {
setMessage(R.string.dialog_msg)
setPositiveButton(R.string.dialog_accept_label, null)
setNegativeButton(R.string.dialog_close_label, null)
setView(dialogView)
}
val alertDialog = builder.create()
val positiveBtn = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
val negativeBtn = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE)
alertDialog.show()
positiveBtn.setOnClickListener {
//do some stuff with data
}
negativeBtn.setOnClickListener {
alertDialog.cancel()
}
But this will throw exception:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference