Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable/enable all children on LinearLayout in Android

Tags:

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?

like image 503
user733284 Avatar asked Oct 07 '11 13:10

user733284


People also ask

How do I make my android layout not touchable?

To make your relativelayout block touches and clicks for all of its children you simply need to set the onTouchListener, like this: YOUR_RELATIVE_LAYOUT. setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // ignore all touch events return true; } });

How do I disable kotlin layout?

To disable all caps through layout file, add textAllCaps attribute to the TextView, android:textAllCaps="false" . To disable all caps programmatically in Kotlin File, set TextView.

How do you get a child of LinearLayout?

You can do like this. ViewGroup layoutCont= (ViewGroup) findViewById(R. id. linearLayout); getAllChildElements(layoutCont); public static final void getAllChildElements(ViewGroup layoutCont) { if (layoutCont == null) return; final int mCount = layoutCont.

How do I stop nested weights on android?

You could use RelativeLayout to avoid the nested weights. Nested weights are bad for performance because the number of measurements increase exponentially with each one nested.


2 Answers

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.
}
like image 52
SBerg413 Avatar answered Sep 21 '22 06:09

SBerg413


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?

like image 38
Parag Chauhan Avatar answered Sep 18 '22 06:09

Parag Chauhan