Android Layout make all children's not clickable

android:clipchildren
addview android
android:descendantfocusability
android:cliptopadding
how to get child position in linearlayout
get all views in activity android
removeview android
get children of viewgroup

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.setClickable(false); but still all the elements inside the layout are clickable.

I know one way of doing it that setting each child element not clickable but it is not an appropriate way because i have lot of child elements like buttons text views etc inside a layout i cannot make each child not clickable.

Here my question is How to set all to setClickable(false); in layout ??

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(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // ignore all touch events
        return true;
    }
});

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).

Android Layout make all children's not clickable, 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; } }); Android Linear Layout - Android LinearLayout is a view group that aligns all children in either vertically or horizontally.

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:

<RelativeLayout
    android:id="@+id/rl_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- the children views begins -->
    ...
    <!-- the children views ends -->

    <LinearLayout
        android: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

ViewGroup, Per-child layout information for layouts that support margins. XML attributes. android:foregroundGravity, Defines the gravity to apply to the foreground drawable. how - Android Layout make all children's not clickable A very simple and full-proof way to do it is to create a sub class and override onInterceptTouchEvent : No need to call any of the children's methods. You can still call setOnClickListener on your myRelativeLayout object.

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

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

FrameLayout, **I try to get all the Webview elements in current activity at the time loading and i MainActivity" android:orientation="vertical"> <LinearLayout  android:layout_width: This is the width of the layout; android:layout_height: This is the height of the layout; android:layout_margin: This is the extra space outside of the view. For example if you give android:marginLeft=20dp, then the view will be arranged after 20dp from left

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

public class MyRelativeLayout extends RelativeLayout {
    @Override
    public boolean onInterceptTouchEvent(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

How to get all child views at runtime? - Mobile, This means that if you have layout B that is nested inside layout A, they will have a child — parent relationship (layout A is the parent of layout B). Use android:layout_width="0px" and android:layout_weight="1" on the three Buttons. That says the buttons should take up no more than 0 pixels, but the three of them should split any extra space between them.

How to make sense of the many Android layouts, The Android Developer Guide. All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot The following two forms represent a LinearLayout with a set of elements: a button, some labels and text boxes. LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the LinearLayout are stacked one after the other, so a vertical list will only have one child per row, no…

Android - Common Layout Objects, To get rid of the tangle for once and for all, I will sum up all the… The LinearLayout has a gravity property which supports centering it's child views. You can  How can I disable all views inside the layout? Ask Question Asked 8 years, 8 months ago. Active 3 months ago. Android Layout make all children's not clickable. 3.

Centering views in Android layouts, At the end of this story, You would get the basic idea about views and layouts in Set android:orientation to specify whether child views are displayed in a row  Note that every LayoutParams subclass has its own syntax for setting values. Each child element must define LayoutParams that are appropriate for its parent, though it may also define different LayoutParams for its own children. All view groups include a width and height (layout_width and layout_height), and each view is required to define them. Many LayoutParams also include optional margins and borders.

Android Beginners : Views & Layouts - Anamika Tripathi, Android LinearLayout is a view group that aligns all children in either vertically or horizontally. Linear Layout. LinearLayout Attributes. Following are the important  android:layout_height. This is the height of the layout. 4: android:layout_marginTop. This is the extra space on the top side of the layout. 5: android:layout_marginBottom. This is the extra space on the bottom side of the layout. 6: android:layout_marginLeft. This is the extra space on the left side of the layout. 7: android:layout_marginRight

Comments
  • What the heck just happened in edits here?
  • Good answer. No performance problem for layouts with too many children.
  • Not exactly what I was inclined to use, but at the end of the day it saves a lot of time. Thanks, +1.
  • In LinearLayout it sets android:clickable="true", shouldn't it be false instead of true to block the children?
  • if the LinearLayout (on top) is clickable that means that everything before its declaration is NOT clickable.