Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable all views inside the layout?

For example I have:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"         android:layout_height="fill_parent">      <Button          android:id="@+id/backbutton"         android:text="Back"         android:layout_width="wrap_content"         android:layout_height="wrap_content" />     <LinearLayout         android:id="@+id/my_layout"         android:orientation="horizontal"         android:layout_width="fill_parent"         android:layout_height="wrap_content">         <TextView             android:id="@+id/my_text_view"             android:text="First Name"             android:layout_width="wrap_content"             android:layout_height="wrap_content" />         <EditText             android:id="@+id/my_edit_view"             android:width="100px"             android:layout_width="wrap_content"             android:layout_height="wrap_content" />          <View .../>         <View .../>         ...         <View .../>     </LinearLayout>  </LinearLayout> 

Is there a way to disable (setEnable(false)) all elements inside LinearLayout my_layout ?

like image 640
Scit Avatar asked Aug 15 '11 18:08

Scit


People also ask

How to disable a layout and its contents programmatically in android?

Each View has "Duplicate parent state" property (in visual editor too). Set it to true for all buttons and other Vies you want. After thet use setEnabled(false); to layout. It should work.

How do I turn off linear layout?

I am able to disable all content inside linear layout using following code: LinearLayout myLayout = (LinearLayout) findViewById(R. id. linearLayout1); for ( int i = 0; i < myLayout.


2 Answers

this one is recursive for ViewGroups

private void disableEnableControls(boolean enable, ViewGroup vg){     for (int i = 0; i < vg.getChildCount(); i++){        View child = vg.getChildAt(i);        child.setEnabled(enable);        if (child instanceof ViewGroup){            disableEnableControls(enable, (ViewGroup)child);        }     } } 
like image 190
tütü Avatar answered Oct 07 '22 02:10

tütü


Another way is to call setEnabled() on each child (for example if you want to do some extra check on child before disabling)

LinearLayout layout = (LinearLayout) findViewById(R.id.my_layout); for (int i = 0; i < layout.getChildCount(); i++) {     View child = layout.getChildAt(i);     child.setEnabled(false); } 
like image 28
4e6 Avatar answered Oct 07 '22 02:10

4e6