How to disable/enable all children on LinearLayout in Android

user733284 picture user733284 · Oct 7, 2011 · Viewed 33.2k times · Source

Is there way by programming that all the children of a certain layout?

For example i have this layout with two children:

<LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:layout_width="fill_parent">
        <SeekBar android:layout_height="wrap_content" android:id="@+id/seekBar1"
            android:layout_weight="1" android:layout_width="fill_parent"></SeekBar>
        <TextView android:id="@+id/textView2" android:text="TextView"
            android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_height="wrap_content"></TextView>
    </LinearLayout>

and i want to do something like:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout1);
myLayout.setEnabled(false);

In order to disable the two textviews.

Any idea how?

Answer

SBerg413 picture SBerg413 · Oct 7, 2011

A LinearLayout extends ViewGroup, so you can use the getChildCount() and getChildAt(index) methods to iterate through your LinearLayout children and do whatever you want with them. I'm not sure what you mean by enable/disable, but if you're just trying to hide them, you can do setVisibility(View.GONE);

So, it would look something like this:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout1);
for ( int i = 0; i < myLayout.getChildCount();  i++ ){
    View view = myLayout.getChildAt(i);
    view.setVisibility(View.GONE); // Or whatever you want to do with the view.
}