Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing information between two alert dialogs

ImageView in first alert dialog opens second dialog to change an imageResource of the ImageView in the first dialog. However I don't know how to make connection between two alert dialogs

Both have different xml layouts, so I assume that in second dialog I should make a reference to the layout of the first dialog

private fun editItemDialog() {
    val dialogBuilder1 = AlertDialog.Builder(this)
    val inflater = this.layoutInflater
    val dialogView = inflater.inflate(R.layout.edit_dialog, null)
    dialogBuilder1.setView(dialogView)

    var editIconButton = dialogView.findViewById<View>(R.id.editIcon) as ImageView

    editIconButton.setOnClickListener{
        showIconDialog()

    }



    dialogBuilder1.setTitle("Edit mode")
    dialogBuilder1.setPositiveButton("Save") { _, _ ->
       //sth
    }
    dialogBuilder1.setNegativeButton("Cancel") { _, _ ->
       //sth
    }
    val b = dialogBuilder1.create()
    b.show()
}     

private fun showIconDialog() {
    val dialogBuilder = AlertDialog.Builder(this)
    val inflater = this.layoutInflater
    val dialogView = inflater.inflate(R.layout.icons, null)
    dialogBuilder.setView(dialogView)

    //examplary two icons to select

    var travelRB = dialogView.findViewById<View>(R.id.travel) as RadioButton

    var travRB = dialogView.findViewById<View>(R.id.travel) as RadioButton


    dialogBuilder.setTitle("Icon dialog")
    dialogBuilder.setMessage("Select an icon")
    dialogBuilder.setPositiveButton("Save") { _, _ ->
         //here I would like to change an icon of the ImageView, for example:
         editIconButton.setImageResource(R.id.travel)

    dialogBuilder.setNegativeButton("Cancel") { _, _ ->
        //sth
    }
    val b = dialogBuilder.create()
    b.show()


}
like image 501
Mark Avatar asked Sep 11 '26 08:09

Mark


1 Answers

You can add a callback to the second dialog

fun showIconDialog(callback : (Drawable) -> Unit) { 
        //code 
        callback.invoke(someDrawable)
    }

And on the first one you just do this:

showIconDialog() { someDrawable ->
        //code to change the layout src icon
    }
like image 88
Diogo Rosa Avatar answered Sep 13 '26 00:09

Diogo Rosa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!