Have next import:
import androidx.appcompat.app.AlertDialog
And create dialog as follows:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val adb = AlertDialog.Builder(activity!!)
.setTitle(R.string.actions_rename_connection)
.setPositiveButton(R.string.actions_ok, this)
.setNegativeButton(R.string.actions_cancel, this)
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_edit_name, null)
val dialog = adb.setView(view).create()
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
return dialog
}
But I'm not satisfied activity!!
.
For example, with another import i create dialog as follows:
import android.app.AlertDialog
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return AlertDialog.Builder(activity, R.style.MaterialThemeDialog)
.setTitle(R.string.in_app_settings_language)
.setPositiveButton(R.string.actions_ok) { _, _ -> presenterContract.onOkClick() }
.setNegativeButton(R.string.actions_cancel) { _, _ -> closeView() }
.create()
}
Q: there are some solutions where we can use activity
in androidx ?
Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.
There are three kinds of lists available with the AlertDialog APIs: A traditional single-choice list. A persistent single-choice list (radio buttons) A persistent multiple-choice list (checkboxes)
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
android.app.AlertDialog use @Nullable activity. IDE is not angry using activity for this reason.
but
androidx.appcompat.app.AlertDialog use @NonNull activity. You should use !! or null check for activity
I think you should use it this way
activity?.let {
AlertDialog.Builder(it, R.style.MaterialThemeDialog)
.setTitle(R.string.in_app_settings_language)
.setPositiveButton(R.string.actions_ok) { _, _ -> presenterContract.onOkClick() }
.setNegativeButton(R.string.actions_cancel) { _, _ -> closeView() }
.create()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With