Search code examples
viewkotlinlayout-inflater

Layout inflater with DialogFragment


I'm trying to inflate my custom layout (a dialog fragment).

I have this in my function showDialog()

    val inflatedView = layoutInflater.inflate(R.layout.alerts_dialog_remi, null)

    mydialog = Dialog(this, R.style.DialogCustomTheme)
    mydialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    mydialog.setContentView(R.layout.alerts_dialog_remi)
    mydialog.setOnShowListener {
        val text = inflatedView.findViewById<TextView>(R.id.alerte_title)
        val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        text.setText("Text")}

        mydialog.create()

        txt = mydialog.findViewById(R.id.close_modal_alerte)
        txt.isEnabled = true
        txt.setOnClickListener{
            mydialog.cancel()
        }
        mydialog.show()
    }

But I don't see the "Text" string in my dialog fragment. I tried the put the inflatedView inside setOnShowListener, but it doesn't do anything either.


Solution

  • You don't need to inflate your view, because dialog.setContentView does just that for you.

    What you need is to get the inflated view from inside your lambda.

    Like this:

     mydialog.setOnShowListener {
        val text = it.view.findViewById<TextView>(R.id.alerte_title)