How to disable/enable all children on LinearLayout in Android
Asked Answered
P

5

22

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?

Persevere answered 7/10, 2011 at 13:15 Comment(0)
J
31

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.
}
Juridical answered 7/10, 2011 at 13:27 Comment(2)
Without using setVisibility(View.GONE) ,it is possible to disable/enable all children on LinearLayout in AndroidWestsouthwest
How do I do it for nested Layout @SBerg413Honaker
W
15

You can also disable/enable without using setVisibility()

Add a View.OnClickListener to your CheckBox then pass the View you want to be disabled into the following function...

private void enableDisableView(View view, boolean enabled) {
    view.setEnabled(enabled);

    if ( view instanceof ViewGroup ) {
        ViewGroup group = (ViewGroup)view;

        for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
            enableDisableView(group.getChildAt(idx), enabled);
        }
    }
}

with the following reference Is there a way to disable all the items in a specific layout programmaticaly?

Westsouthwest answered 15/3, 2012 at 8:28 Comment(1)
Blatant copy from https://mcmap.net/q/587256/-is-there-a-way-to-disable-all-the-items-in-a-specific-layout-programmatically, you could at least have given credit.Concettaconcettina
G
7

Why not just doing the setVisibility(View.GONE) on the layout itself, rather than iterating through the children?

Gent answered 7/10, 2011 at 15:10 Comment(1)
You might have a custom view containing some other customs views. You might want to hide the random views hanging on your custom view but not the views in the custom view itself. It's pretty common on libraries for example. If you hide the custom view you are hiding everything, not just the childs added by the users of your library.Calci
M
4

Maybe is late, but you can just add android:duplicateParentState="true" in the child of the LinearLayout

Something like this:

 <LinearLayout
     android:id="@+id/container"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">

     <TextView
         android:id="@+id/textA"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:duplicateParentState="true"
         android:text="First Text"/>

     <TextView
         android:id="@+id/textB"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:duplicateParentState="true"
         android:text="Second Text"/>

 </LinearLayout>
Mammalian answered 5/12, 2018 at 17:10 Comment(0)
F
1

Just add another transparent layout that will match_parent of the original view and change it visibility to visible when you want to disable all the children and to enable children then just change visibility to gone

Franzen answered 16/6, 2015 at 9:6 Comment(2)
Could you please elaborate more your answer adding a little more description about the solution you provide?Elidiaelie
you can check here #10481060Franzen

© 2022 - 2024 — McMap. All rights reserved.