14

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?

NeoWang
  • 17,361
  • 24
  • 78
  • 126
  • Possible duplicate of [How to disable behind view click event Framelayout](https://stackoverflow.com/questions/16377593/how-to-disable-behind-view-click-event-framelayout) – Mehdi Dehghani May 15 '18 at 06:53
  • Does this answer your question? [Android: How to prevent any touch events from being passed from a view to the one underneath it?](https://stackoverflow.com/questions/8433387/android-how-to-prevent-any-touch-events-from-being-passed-from-a-view-to-the-on) – General Grievance Sep 13 '22 at 17:44

4 Answers4

37

If overlay doesn't get touch events, then the events are passed to underlying view, so to block touch events in views below it, make your overlay touchable. android:clickable="true"

Gregor Ažbe
  • 353
  • 8
  • 12
PPD
  • 5,660
  • 12
  • 52
  • 86
9

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>
Blesson Jose
  • 698
  • 9
  • 14
5

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.

Ângelo Polotto
  • 8,463
  • 2
  • 36
  • 37
1

put the button on overlay layer.then set that button android:background="@null" it block touch event of view below it..hope it solve your problem

Iyyanarappan
  • 145
  • 1
  • 7