Neil Neil - 1 year ago 90
Android Question

Android: How to find views by type

Ok so I have a layout xml similar to the following example:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android=""
android:background="@drawable/tile_bg" >

android:paddingTop="10dp" >

android:orientation="horizontal" >

<!-- view stuff here -->

<!-- more linear layouts as siblings to this one -->


I actually have about 7 LinearLayout items each with id increasing from layout_0 etc. I want to be able to get hold of all the LinearLayout items under the root LinearLayout. Do I need to put an id on the root one and find all others by id or can I get them by type.

The code I use to inflate the layout is:

View view = (View) inflater.inflate(R.layout.flight_details, container, false);

I read somewhere that you can iterate children of a ViewGroup but this is only a View.

What is the best way to get a bunch of children by type?

Answer Source

This should get you on the right track.

LinearLayout rootLinearLayout = (LinearLayout) findViewById(;
int count = rootLinearLayout.getChildCount();
for (int i = 0; i < count; i++) {
    View v = rootLinearLayout.getChildAt(i);
    if (v instanceof LinearLayout) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download