Disable the Touch Events for All the Views

How to disable all user inputs (click, touch) on an android view

In your GameView implement onTouchEvent() like this.

public class GameView extends View {

public boolean isTouchable() {
return isTouchable;
}

public void setTouchable(boolean isTouchable) {
this.isTouchable = isTouchable;
}

private boolean isTouchable= true;

public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public GameView(Context context) {
super(context);

}

//// ... Your Code

@Override
public boolean onTouchEvent(MotionEvent event) {
if(isTouchable){
return super.onTouchEvent(event); // Enable touch event
}
return false; // Block touch event
}

}

How to use?

gameView.setTouchable(false); // to disable touch

gameView.setTouchable(true); // to enable touch

How to disable touch events on a View?

Generally, you will want the splash screen to prevent the user from doing anything while it is present. You can actually do this, because views in android, as in other UI frameworks, have a hierarchy in which touch events cascade.

Even though you don't want the splash screen to be interactive, you can still steal all of the touch events that occur on it, and just do nothing with them. The answer itself is to override onTouchEvent in your SplashScreen class with:

@Override
public boolean onTouchEvent(MotionEvent event) {
return true; // Splash screen is consuming all touch events on it.
}

Returning true in this basically says that nothing under this view should receive a touch event. As long as it is visible and over top of the surface view, your surface view will not get any touch events. See here for details.

How can I disable all touch events on all children of a ViewGroup?

May be I have misunderstood something, but I guess the answer to your question is very simple:
If you don't want to dispatch none of the touch events to the children - so you just need to override ViewGroup.onInterceptTouchEvent (MotionEvent ev) API reference like this:

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

According to the android documentation this means that your ViewGroup will always intercept all the touch events and don't dispatch them to the children. All of them will be directed to the ViewGroup.onTouchEvent(MotionEvent ev) method of your ViewGroup.

Disable/filter any touch event for a Layout

  1. Disable all touch event in parent view. dispatchTouchEvent() and
    onInterceptTouchEvent() can all achieve this goal.
  2. Set android:duplicateParentState="true" to all child view, and set
    parent android:enable="false"

Android: How to prevent any touch events from being passed from a view to the one underneath it?

Add an onTouchEvent method to the view with top position then return true. True will tell the event bubbling that the event was consumed therefore prevent event from bubbling to other views.

protected boolean onTouchEvent (MotionEvent me) {
return true;
}

For v1 you would do an import:

import android.view.View.OnTouchListener;

Then set the onTouchListener:

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


Related Topics



Leave a reply



Submit