Skip to content Skip to sidebar Skip to footer

Android Layout Make All Children's Not Clickable

I am using Relative Layout and many buttons in it with TextViews etc.I want to make all of them not clickable unless some event happens. I tried setting RelativeLayout.setClickab

Solution 1:

When you say click do you actually mean touch? Touch events are done element by element. They're first sent to the top level, which says if it handled it and if it didn't it goes onto each children of the view.

When a touch event is created, the onTouch method of view in the chain is called, if any of these return true (true meaning "I handled this!") it stops going down to the children.

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(newOnTouchListener() {
    @OverridepublicbooleanonTouch(View v, MotionEvent event) {
        // ignore all touch eventsreturntrue;
    }
});

This will ignore all touch events happening on the relative layout (and all of its children) which includes simple touch down then release events (called clicks).

Solution 2:

I found an alternative way to achieve this. You may create a blocking LinearLayout on top all its sibling views in the RelativeLayout like below:

<RelativeLayoutandroid:id="@+id/rl_parent"android:layout_width="match_parent"android:layout_height="match_parent"><!-- the children views begins -->
    ...
    <!-- the children views ends --><LinearLayoutandroid:id="@+id/ll_mask"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/transparent"android:clickable="true"android:orientation="horizontal"android:visibility="visible"/></RelativeLayout>

Later you may toggle the LinearLayout's visibility to GONE to allow the children views to be clickable again:

mMask.setVisibility(View.VISIBLE); // blocks children
mMask.setVisibility(View.GONE); // children clickable

Solution 3:

You can use following function to find all the child view and cancel click.

publicvoidsetClickable(View view) {
    if (view != null) {
        view.setClickable(false);
        if (view instanceof ViewGroup) {
            ViewGroupvg= ((ViewGroup) view);
            for (inti=0; i < vg.getChildCount(); i++) {
                setClickable(vg.getChildAt(i));
            }
        }
    }
}

Solution 4:

A very simple and full-proof way to do it is to create a sub class and override onInterceptTouchEvent:

publicclassMyRelativeLayoutextendsRelativeLayout {
    @OverridepublicbooleanonInterceptTouchEvent(MotionEvent ev) {
        // true if you do not want the children to be clickable.return mShouldInterceptAllTouch;
    }
}

No need to call any of the children's methods.

You can still call setOnClickListener on your myRelativeLayout object. Also, you can use the class in XMLs as if you were using a RelativeLayout

Solution 5:

An easy Kotlin extension solution to disable/enable a view and all of it's children:

fun View.isUserInteractionEnabled(enabled: Boolean) {
    isEnabled = enabled
    if (thisis ViewGroup && this.childCount > 0) {
        this.children.forEach {
            it.isUserInteractionEnabled(enabled)
        }
    }
}

and call it with:

view.isUserInteractionEnabled(false)

Post a Comment for "Android Layout Make All Children's Not Clickable"