Search code examples
c#androidxamarin.android

Loop through child views created dynamically in android view


I have a view where I have added several EditText views dynamically and I need to loop through the views to capture any data entered. I've looked at this solution on StackOverflow:

public void recursiveLoopChildren(ViewGroup parent) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            final View child = parent.getChildAt(i);
            if (child instanceof ViewGroup) {
                recursiveLoopChildren((ViewGroup) child);
                // DO SOMETHING WITH VIEWGROUP, AFTER CHILDREN HAS BEEN LOOPED
            } else {
                if (child != null) {
                    // DO SOMETHING WITH VIEW
                }
            }
        }
    }

But this doesn't work because ViewGroup does not contain a method GetChildCount(). I've looked at the definition for ViewGroup and View and neither one has a "count" or "getchildcount" method or property. Is there a way to loop through the child views of a view?


Solution

  • Well, neither ViewGroup or View have a child count, but LinearLayout does. Given that, I was able to get it done.

    This is my Layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="wrap_content"
                android:orientation="horizontal"
                android:layout_height="wrap_content"
                android:layout_marginRight="34.4dp">
                <Button
                    android:layout_marginLeft="3dp"
                    android:background="@color/colorGold"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/saveChange"
                    android:id="@+id/btnSave" />
          </LinearLayout>
        </HorizontalScrollView>
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_gravity="center"
            android:layout_marginTop="0sp"
            android:background="#000000" />
        <ImageView
            android:contentDescription="Participant's Photo"
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:id="@+id/imageview_participant"
            android:layout_marginBottom="0.0dp" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:gravity="center"
            android:id="@+id/TextView_name" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold"
                android:layout_width="230dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:id="@+id/ageasof"
                android:text="@string/ageAsOf" />
            <TextView
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/age"
                />
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_gravity="center"
            android:layout_marginTop="0sp"
            android:background="#000000" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:gravity="center"
            android:text="@string/AdditionalData" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/listlayout">
        </LinearLayout>
    </LinearLayout>
    

    As you can see I have three LinearLayouts of which only one (listlayout) has the subviews I'm looking for because they are added to the listlayout LinearLayout at run time. I am able to loop through each of the LinearLayouts and access the EditTexts contained within. I use a button click to save the data.
    My code:

    btnSave.Click += (sender, e) =>
    {
        for (int i = 0; i < list.ChildCount; i++)
        {
            object MyObject = list.GetChildAt(i);
            if (MyObject.GetType() == typeof(LinearLayout))
            {
                LinearLayout MyLinearLayout = (LinearLayout)MyObject;
                for (int j= 0; j < MyLinearLayout.ChildCount; j++)
                {
                    object MyObject2 = MyLinearLayout.GetChildAt(j);
                    if (MyObject2.GetType() == typeof(EditText))
                    {
                        EditText MyEditText = (EditText)MyObject2;
                        datainterface.SaveDataFieldData(MyUser.Username, MyUser.Password, fieldid, (string)MyEditText.Tag, MyEditText.Text);
                    }
                }
            }
        }
    };
    

    I store the data field ID in the tag of the EditText so I can line up the data with the correct field when I retrieve it for edit. And yes, I can optimize the code further, but wanted to post it as is so it is more readable. In any event, works like a charm.