Search code examples
androidkotlindialogfloating-action-button

why Dialog doesn't show in Kotlin?


I want to create a Dialog window when I click the Floating Action Button. But, when I click the button, just appears the Toast message.

This is what I've tried so far:

    recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
    val users = ArrayList<User>()

    users.add(User("John", "USA"))

    val adapter = CustomAdapter(users)

    recyclerView.adapter = adapter

    fab.setOnClickListener {
        val dialog = Dialog(this)
        Toast.makeText(this, "It's working...", Toast.LENGTH_LONG).show()
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(R.layout.dialog_add)
        dialog.setTitle("Add person")
        dialog.setCancelable(false)

        val nameText = dialog.findViewById(R.id.name) as EditText
        val addressText = dialog.findViewById(R.id.address) as EditText
        val btnAdd = dialog.findViewById(R.id.btn_ok) as Button
        val btnCancel = dialog.findViewById(R.id.btn_cancel) as Button

        btnAdd.setOnClickListener{
            users.add(User(nameText.text.toString(), addressText.text.toString()))
            adapter.notifyDataSetChanged()
            dialog.dismiss()
        }
        btnCancel.setOnClickListener {
            dialog.dismiss()
        }
    }
}

How can I change the code so it show the Dialog Window when I click the FAB?

UPDATE: You guys're right! It worked just fine after I put dialog.show(). Thank you.


Solution

  • You forgot to invoke show() on dialog. dialog.show()