why Dialog doesn't show in Kotlin?

35
October 09, 2019, at 2:20 PM

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?

Answer 1

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

Answer 2

You need to call dialog.show(). Just call it anywhere below dialog.setCancelable(false).

Answer 3

After create dialog , we need to call show method to show dialog on screen

Add This line after create dialog dialog.show()

Please replace or add this code

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()
        }
    //add this line 
     //Call show() method to show dialog 
  dialog.show()
    }
}
Answer 4

You have to invoke show() like the following.

//...
dialog.setTitle("Add person")
dialog.setCancelable(false)
// ...
btnAdd.setOnClickListener{
    //...
}
btnCancel.setOnClickListener {
    dialog.dismiss()
}
//show dialog adding below line.
dialog.show();
READ ALSO
How to custom claims token to firebase firestore rules via Android App?

How to custom claims token to firebase firestore rules via Android App?

I'm trying to customize claims token to firestore rule via Cloud Functions but it do not appear a good solution because it throws an exception when I try create an user via Android Application like follow:

41
Cordova ble.write i can&#39;t send data (Android)

Cordova ble.write i can't send data (Android)

this function writes data to a characteristic

13
Using ScheduledExecutorService to start and stop ToneGenerator linked to timer

Using ScheduledExecutorService to start and stop ToneGenerator linked to timer

I'm using ScheduledExecutorService to play a tone every 2 seconds that a CountDownTimer is runningWhen the CountDownTimer is paused or reset, I need the tone to stop playing

26
Toolbar overflow menu sometimes aligned to left side of screen

Toolbar overflow menu sometimes aligned to left side of screen

My app has multiple Fragments and a custom Toolbar, on the MainActivityThe toolbar is inflated on my 1st fragment but not the other fragments

41