Disable the touch events for all the views

AndroidAndroid ViewTouch Event

Android Problem Overview


What's the best way to disable the touch events for all the views?

Android Solutions


Solution 1 - Android

Here is a function for disabling all child views of some view group:

 /**
   * Enables/Disables all child views in a view group.
   * 
   * @param viewGroup the view group
   * @param enabled <code>true</code> to enable, <code>false</code> to disable
   * the views.
   */
  public static void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
    int childCount = viewGroup.getChildCount();
    for (int i = 0; i < childCount; i++) {
      View view = viewGroup.getChildAt(i);
      view.setEnabled(enabled);
      if (view instanceof ViewGroup) {
        enableDisableViewGroup((ViewGroup) view, enabled);
      }
    }
  }

Solution 2 - Android

Override the dispatchTouchEvent method of the activity and like this:

@Override
public boolean dispatchTouchEvent(MotionEvent ev){      
  return true;//consume
}

If you return true all touch events are disabled.

Return false to let them work normally

Solution 3 - Android

You could try:

your_view.setEnabled(false);

Which should disable the touch events.

alternatively you can try (thanks to Ercan):

@Override
public boolean dispatchTouchEvent(MotionEvent ev){      
  return true;//consume
}

or

public boolean dispatchTouchEvent(MotionEvent ev) {
    if(!onInterceptTouchEvent()){
        for(View child : children){
            if(child.dispatchTouchEvent(ev))
                return true;
        }
    }
    return super.dispatchTouchEvent(ev);
}

Solution 4 - Android

This piece of code will basically propagate this event to the parent view, allowing the touch event, if and only if the inProgress variable is set to false.

private boolean inProgress = false;

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (!inProgress)
        return super.dispatchTouchEvent(ev);
    return true;
 }

Solution 5 - Android

Use this. returning true will indicate that the listener has consumed the event and android doesn't need to do anything.

view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});

Solution 6 - Android

What about covering a transparent view over all of your views and capturing all touch event?

Solution 7 - Android

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                     WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

Solution 8 - Android

In Kotlin:

fun View.setEnabledRecursively(enabled: Boolean) {
    isEnabled = enabled
    if (this is ViewGroup)
        (0 until childCount).map(::getChildAt).forEach { it.setEnabledRecursively(enabled) }
}

// usage
import setEnabledRecursively

myView.setEnabledRecursively(false)

Solution 9 - Android

The easiest way to do this is

private fun setInteractionDisabled(disabled : Boolean) {
  if (disabled) {
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
  } else {
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
  }
} 

Solution 10 - Android

I made this method, which works perfect for me. It disables all touch events for selected view.

public static void disableView(View v) {

    v.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });

    if (v instanceof ViewGroup) {
        ViewGroup vg = (ViewGroup) v;
        for (int i = 0; i < vg.getChildCount(); i++) {
            View child = vg.getChildAt(i);
            disableView(child);
        }
    }
}

Solution 11 - Android

It may not be possible for the whole application. You will have to override onTouchEvent() for each view and ignore the user inputs.

Solution 12 - Android

Per your comment:

> i just want to be able to disable the views of the current activity at some point

you seem to want to disable all touch for the current activity regardless of the view touched.

Returning true from an override of Activity.dispatchTouchEvent(MotionEvent) at the appropriate times will consume the touch and effectively accomplish this. This method is the very first in a chain of touch method calls.

Solution 13 - Android

This worked for me, I created an empty method and called it doNothing.

   public void doNothing(View view)
{

}

Then called this method from onClick event on all the objects I wanted to disable touch event. android:onClick="doNothing"

When the click or touch event is fired nothing is processed.

Solution 14 - Android

One more easier way could be disabling it through layout (i.e. .xml) file: Just add

 android:shouldDisableView="True"

for the view you want disable touch events.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGratziView Question on Stackoverflow
Solution 1 - AndroidpecepsView Answer on Stackoverflow
Solution 2 - AndroidErcanView Answer on Stackoverflow
Solution 3 - AndroidOWADVLView Answer on Stackoverflow
Solution 4 - AndroidBruno HortaView Answer on Stackoverflow
Solution 5 - AndroidAbhishekView Answer on Stackoverflow
Solution 6 - AndroidYudiView Answer on Stackoverflow
Solution 7 - AndroidIndra KView Answer on Stackoverflow
Solution 8 - AndroidyanchenkoView Answer on Stackoverflow
Solution 9 - AndroidShivam DawarView Answer on Stackoverflow
Solution 10 - Androidjan hruskaView Answer on Stackoverflow
Solution 11 - AndroidRajathView Answer on Stackoverflow
Solution 12 - AndroidSK9View Answer on Stackoverflow
Solution 13 - AndroidKizito IkapelView Answer on Stackoverflow
Solution 14 - AndroidBhanuSinghView Answer on Stackoverflow