35

I have a layout that contains many views. Is there an easy way to disable all its views click events?

10 Answers 10

53

You can pass View for disable all child click event.

public static 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);
            }
        }
    }
4
  • 2
    @FARID pass enableDisableView(linView,false); it should work Commented Jun 5, 2017 at 6:11
  • Worked like a charm. Tested on github.com/roughike/BottomBar to disable tabs Commented Nov 23, 2017 at 8:43
  • @Mr.Robot Enjoy buddy Happy coding . :) Commented Nov 24, 2017 at 9:46
  • @Parag Chauhan what is "linView" in the enableDisableView() above? It is a child view of the ViewGroup?
    – AJW
    Commented Dec 15, 2021 at 18:18
34

Rather than iterating through all the children view, you can add this function to the parent Layout view

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return true;
}

This will be called before the onTouchEvent for any child views, and if it returns true, the onTouchEvent for child views wont be called at all. You can create a boolean field member to toggle this state on and off if you want.

2
  • Not working for me, you mean add this method inside the Activity and it will do the job? I can still click on Spinners and Checkbox Commented Mar 19, 2019 at 20:32
  • 1
    @Skizo-ozᴉʞS inherit the parent view class and add that method to the new class and use that class in your xml. Commented May 8, 2019 at 13:48
24

I would create a ViewGroup with all the views that you want to enable/disable at the same time and call setClickable(true/false) to enable/disable clicking.

1
  • 1
    And by ViewGroup i mean one of the subclasses. Check this link
    – khellang
    Commented Jun 4, 2011 at 19:36
15

Here is a Kotlin extension function implementation of Parag Chauhan's answer

fun View.setAllEnabled(enabled: Boolean) {
    isEnabled = enabled
    if (this is ViewGroup) children.forEach { child -> child.setAllEnabled(enabled) }
}
2
  • 1
    great answer! Works great with custom child views! Commented Sep 26, 2019 at 15:05
  • 2
    Just a heads up @letterman549, If you have a RecyclerView in your ViewGroup this will not work for all the child items. It will appear to work for the first few items of the RecyclerView, but as you scroll the views will not all be set to the specified boolean. Commented Sep 26, 2019 at 18:20
3

You need to call setEnabled(boolean value) method on the view.

view.setClickable(false);
view.setEnabled(false);
1
  • 16
    Not works for me, the childrens still having actions
    – jose920405
    Commented Sep 23, 2016 at 22:24
0

If you dont want to set all child views to disable state (because they may look different to enable state) you can use this approach:

private fun toogleTouchable(canTouch: Boolean) {
        container.descendantFocusability =
                if (value) {
                    container.requestFocus()
                    ViewGroup.FOCUS_BLOCK_DESCENDANTS
                } else {
                    ViewGroup.FOCUS_AFTER_DESCENDANTS
                }
    }
0

my layout has two modes. One preview, when children should not be clickable and the other when children should be clickable. Of all the solutions mentioned here, I found overriding onInterceptTouchEvent most appropriate. Very little code, with no iteration on children.

public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(isPreviewMode())
        {
            //No child is clickable in preview mode.
            return true;
        }

         //All children are clickable otherwise
        return false;
    }

Though disabling children is most popular solution, I am not confortable with that.

You may need to reEnable them and the logic should match exactly. The more is the code, the higher the chances of bugs in the future.

0

Extension function in Kotlin, with the support of Recycler view.

fun ViewGroup.setEnabledStateForAllChildren(enable: Boolean) {
    children.forEach {
        when (it) {
            is RecyclerView -> {
                it.addOnItemTouchListener(object : SimpleOnItemTouchListener() {
                    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
                        return enable.not()
                    }
                })
            }
            is ViewGroup -> {
                it.setEnabledStateForAllChildren(enable)
            }
            else -> {
                it.isEnabled = enable
            }
        }
    }
}
0

My approach for this issue is adding another View above the layout with the same size. This extra View is a overlay. When you decide to disable layout interaction you display this overlay View, to enable you hide it.

<FrameLayout ...>

    <!-- This is your layout -->
    <YourLayout 
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         ...>
      

       ...
    <YourLayout/>

    <!-- This is your overlay -->
    <View
         android:id="@+id/overlay"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:clickable="true"
         android:focusable="true"
         android:background="#80FFFFFF">
    <!-- semi transparent background color is optional, for indicating interaction is not enabled -->


</FrameLayout>

In your Activity/Fragment:

// disable interaction of the layout
binding.overlay.visibility = View.VISIBLE

// enable interaction of the layout
binding.overlay.visibility = View.GONE
-4

I would implement onClickListener interface in your activity class and return false in onClick method. I feel it's the easiest way to solve your problem.

1
  • 11
    return type for onClick is void
    – amiekuser
    Commented Jun 13, 2013 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.