Getting child elements from LinearLayout

Viewed 72729

Is there a way to obtain a child element of a LinearLayout? My code returns a view (linearlayout), but I need to get access to specific elements inside of the layout.

Any suggestions?

(Yes, I know I could use findViewById, but I am creating the layouts/children in java - not XML.)

7 Answers

The solution in Kotlin version will be:

val layout: LinearLayout = setupLayout()
        val count = layout.childCount
        var v: View? = null
        for (i in 0 until count) {
            v = layout.getChildAt(i)
            //do something with your child element
        }

One of the very direct ways is to access the child views with their ids.

view.findViewById(R.id.ID_OF_THE_CHILD_VIEW)

here view is the parent view.

So for instance your child view is an imageview, with id, img_profile, what you can do is:

ImageView imgProfile = view.findViewById(R.id.img_profile)
Related