Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create AlertDialog in androidx.appcompat

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 ?

like image 973
Morozov Avatar asked May 16 '19 11:05

Morozov


People also ask

What is AlertDialog message?

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.

How many styles of AlertDialog are there?

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)

What is the difference between dialog and AlertDialog?

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 .


1 Answers

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()   
}
like image 59
Cafer Mert Ceyhan Avatar answered Oct 12 '22 20:10

Cafer Mert Ceyhan