LeTadas
LeTadas

Reputation: 3556

Kotlin DialogFragment editText editable always null

So, I'm using Kotlin extensions which is straightforward, but I can't get string from edittext

here is my code:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

    val v = activity.layoutInflater
            .inflate(R.layout.dialog_group, null)

    v.add_group_button.setOnClickListener(addListener)

    return AlertDialog.Builder(activity)
            .setView(v)
            .create()
}

private var addListener: View.OnClickListener = View.OnClickListener {

        val groupNameInput: String = view?.group_edit_text?.text.toString()

    }

when I'm pressing add button groupNameInput always returns null why?

Upvotes: 1

Views: 900

Answers (1)

LeTadas
LeTadas

Reputation: 3556

So finally I figure it out — on Dialog Fragment view will always be null, because its never created, but it's created and added to dialog view which means I need to call:

dialog.group_edit_text.text.toString()

Instead of:

view.group_edit_text.text.toString()

Upvotes: 2

Related Questions