1

I have an AlertDialog box that I am using to get input from the user. But when the user clicks outside the dialog, it is getting dismissed irrespective of whether the user has entered the input or not.

That input is very crucial for further processes in the app. So I've to keep it.
I would really appreciate a different approach as long as it fulfills my purpose.

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
Levi
  • 187
  • 2
  • 17

2 Answers2

6

You just have to invoke method setCanceledOnTouchOutside(), as below:

dialog.setCanceledOnTouchOutside(false);

Happy coding!

HaroldSer
  • 2,025
  • 2
  • 12
  • 23
  • 1
    using this I am getting Unresolved reference error. But using `dialog.setCancelable(false)` worked just fine – Levi Oct 31 '20 at 09:09
1

Here is a simple dialog which cannot be canceled by clicking outside it:

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // create dialog
        val builder = AlertDialog.Builder(this).apply {

            setPositiveButton("Ok",
                              DialogInterface.OnClickListener { dialog, id ->
                                  // User clicked OK button
                              })

        }

        //This will make dialog unCancelable
        val alertDialog: AlertDialog = builder.setCancelable(false).create()

        // show dialog
        alertDialog.show()
    }
}
iknow
  • 8,358
  • 12
  • 41
  • 68