Ivan España
Ivan España

Reputation: 1

Get text from EditText within for loop in Kotlin

I'm developing an app in Android in Kotlin, and in one activity I have 10 editText. How i could get the text of all editTexts within a for loop? EditTexts are into Constraint Layouts, which are into a Linear Layout.enter image description here

Upvotes: 0

Views: 173

Answers (1)

cylon
cylon

Reputation: 733

You can iterate over the child views of the parent view group and just gather the texts, something like this:

val parentView: ViewGroup = findViewById(R.id.parent)
for (i in 0 until parentView.childCount) {
    val view: View = parentView.getChildAt(i)
    if (view is EditText) {
        Log.d("text", view.text.toString())
    }
}

Change the R.id.parent to the correct id of course - the id of your constraint layout.

Upvotes: 2

Related Questions