Using CustomLayout on AlertDialog

Viewed 33

I want to use a simple AlertDialog for my application because I need it to use two or more EditText's on my AlertDialog. This is why I'm using the custom layout on the AlertDialog. Everything has been set, but I couldn't get data from my EditText fields. Probably I'm missing something which is really easy, but it might be a bigger problem, this is why I'm asking this question. Here's the code block.

 val alert = AlertDialog.Builder(activity)
    val layout = R.layout.alert_view
    val customLayout: View = layoutInflater.inflate(layout, null)
    alert.setView(customLayout)
    var etCourse1 = view?.findViewById<EditText>(R.id.etCourse1)
    var etCourse2 = view?.findViewById<EditText>(R.id.etCourse2)
    alert.setIcon(R.drawable.ic_warning_black_24dp)

    alert.setPositiveButton("Confirm")
    {
            dialog, _ ->
        tempEditTextValue = etCourse1?.text.toString() //editText.text.toString()
        tempEditTextValue2 = etCourse2?.text.toString()
        if(tempEditTextValue.isEmpty())
        {
            Toast.makeText(activity,"TextField is Empty", Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }
        else
        {

            //FunctionHere
        }

    }
    alert.setNegativeButton("Cancel")
    {
            dialog, _ ->
        dialog.dismiss()
    }
    alert.setCancelable(false)


    alert.show()

Also, could I use custom buttons to use as Confirm and Cancel buttons? Thanks!

1 Answers

My view is customLayout but I'm trying to use view to get that EditText's.

EditText's must be like;

    var etCourse1 = customLayout?.findViewById<EditText>(R.id.etCourse1)
    var etCourse2 = customLayout?.findViewById<EditText>(R.id.etCourse2)
Related