How to take a list of identical view components

Is it possible to somehow take an array, for example, a TextView, which are in the current Activity, or is it necessary to take each view?

Upvotes: 0

Views: 32

Answers (1)

Umair Anjum
Umair Anjum

Reputation: 67

You can get all the views of your xml programmatically as below:

  • bind your ViewGroup / Parent layout

  • Then the child View are accessible in if condition

    val container = findViewById(R.id.container) as ViewGroup

         for (i in 0 until container.childCount) {
             val v = container.getChildAt(i)
             if (v is Button) {
                 // You will get Button here
             }
             else if(v is TextView){
                 // You will get textView here
             }         
         }
    

Upvotes: 1

Related Questions