0

Here's the AlertDialog inside the MainActivity onCreate method:

import kotlinx.android.synthetic.main.dialog.*

class MainActivity : AppCompatActivity() {

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

        fab.setOnClickListener { view ->
            AlertDialog.Builder(this@MainActivity)
                    .setView(LayoutInflater.from(this@MainActivity).inflate(R.layout.dialog, null))
                    .setPositiveButton("OK") {dialog, i ->
                        Toast.makeText(
                                this@MainActivity, dialogEditText.text, Toast.LENGTH_SHORT)
                                .show()
                        dialog.dismiss()
                    }.show()
        }
    }
}

Here's the dialog layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText
        android:id="@+id/dialogEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>
</RelativeLayout>

However, in the end, I got this error:

java.lang.IllegalStateException: dialogEditText must not be null

But I filled the input... What's wrong here?

matolaypal
  • 29
  • 6

2 Answers2

2

This is the solution what I found. Thanks for helping!

    val view = LayoutInflater.from(this@MainActivity).inflate(R.layout.dialog, null)
    AlertDialog.Builder(this@MainActivity)
            .setView(view)
            .setPositiveButton("OK") {dialog, i ->
                Toast.makeText(
                        this@MainActivity, view.dialogEditText.text, Toast.LENGTH_SHORT)
                        .show()
                dialog.dismiss()
            }.show()
matolaypal
  • 29
  • 6
-1
 import kotlinx.android.synthetic.main.dialog.*
    val dialogBuilder = AlertDialog.Builder(this)
        val inflater = this.layoutInflater
        val dialogView = inflater.inflate(R.layout.dialog, null)
        dialogBuilder.setView(dialogView)
        val editText = dialogView.findViewById<View>(R.id.dialogEditText) as EditText
        dialogBuilder.setTitle("Your Dialog")
        dialogBuilder.setPositiveButton("OK", DialogInterface.OnClickListener { dialog, whichButton ->
           Toast.makeText(this@MainActivity, editText.text.toString(), Toast.LENGTH_SHORT)
                                    .show() 
                                     dialog.dismiss()

        })
        val b = dialogBuilder.create()
        b.show()
santosh kumar
  • 2,952
  • 1
  • 15
  • 27
  • This doesn't really help – donfuxx Jan 29 '18 at 13:23
  • Yeah, okay. But the point is that I need the input value (as String) from the custom dialog EditText. – matolaypal Jan 29 '18 at 13:25
  • You are using dialogEditText which is not found anywhere in your code. – santosh kumar Jan 29 '18 at 13:40
  • You can use extensions because of kotlin. Check my dialog layout what I shared. The dialogEditText id is there and I imported the layout at the start of MainActivity. (And you can use lambda instead of DialogInterface.OnClickListener) But thanks for the helping! I found the mistake (see my answers). – matolaypal Jan 29 '18 at 13:53
  • May be a way of using it thanks. But I feel my answer should not be downvoted. – santosh kumar Jan 29 '18 at 14:08