Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an overlay that blocks touch events to UI below it?

I used a layer of framelayout with a semi-translucent background to create an overlay. But this overlay doesn't block touch events to interact with the views below it. How should create an overlay that blocks all touch events?

like image 435
NeoWang Avatar asked Jan 03 '15 08:01

NeoWang


People also ask

How to intercept touch event android?

Intercept touch events in a ViewGroup. The onInterceptTouchEvent() method is called whenever a touch event is detected on the surface of a ViewGroup , including on the surface of its children.

Which method you should override to control your touch action in Android?

1.1. You can react to touch events in your custom views and your activities. Android supports multiple pointers, e.g. fingers which are interacting with the screen. The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. you override the onTouchEvent() method.

What is onInterceptTouchEvent?

This is basically a hit testing algorithm where you figure out which child view's bounding rectangle contains the touch point coordinates. But before it can dispatch the event to the appropriate child view, the parent can spy and/or intercept the event all together. This is what onInterceptTouchEvent is there for.

How are Android touch events delivered?

The touch event is passed in as a MotionEvent , which contains the x,y coordinates, time, type of event, and other information. The touch event is sent to the Window's superDispatchTouchEvent() .


2 Answers

Improving the Blesson Jose answer, you need set the android:focusable="true" and android:clickable="true" if you are using View to block the touch.

like image 172
Ângelo Polotto Avatar answered Oct 13 '22 01:10

Ângelo Polotto


Below code has worked for me. I've added android:clickable="true" to block touch events to other views below it.

This is an example with a ProgressBar inside the overlay. If you don't want the ProgressBar, you can use the FrameLayout without it.

<FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/progress_overlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.4"
            android:animateLayoutChanges="true"
            android:background="@android:color/black"
            android:visibility="gone"
            android:clickable="true">

            <ProgressBar
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:indeterminate="true"/>

</FrameLayout>
like image 44
Blesson Jose Avatar answered Oct 13 '22 01:10

Blesson Jose