What\'s the best way to disable the touch events for all the views?
What about covering a transparent view over all of your views and capturing all touch event?
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
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.
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.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
The easiest way to do this is
private fun enableDisableActivty(isEnable : Boolean){
if(!isEnable) window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
else window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
}