Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To create an overlay view, when setting up the <code>LayoutParams</code> you need to set the type to <code>TYPE_SYSTEM_OVERLAY</code> and use the flag <code>FLAG_WATCH_OUTSIDE_TOUCH</code>. This presents a problem because as the Android documentation states: </p> <blockquote> <p>you will not receive the full down/move/up gesture, only the location of the first down as an <code>ACTION_OUTSIDE</code>.</p> </blockquote> <p>In order to receive the full array of touch events you need to use the <code>TYPE_SYSTEM_ALERT</code> type, but this causes the overlay to take over the screen and stop interaction with other elements. The solution is to use both <code>TYPE_SYSTEM_OVERLAY</code> and <code>TYPE_SYSTEM_ALERT</code> and switch between them by changing the type of the <code>LayoutParams</code> as needed.</p> <p>This is accomplished by:</p> <ol> <li>Watch for the <code>ACTION_OUTSIDE</code> motion event.</li> <li>When it occurs, test if it occured within the overlay.</li> <li>If it did, switch the <code>LayoutParams</code> type to <code>TYPE_SYSTEM_ALERT</code></li> <li>Once the interaction with the overlay is complete, switch back to <code>TYPE_SYSTEM_OVERLAY</code></li> <li>Repeat</li> </ol> <p>The one thing to keep in mind is that the <code>ACTION_OUTSIDE</code> motion event is always passed on to the rest of the elements on the screen. So, for example, if the overlay is on top of a button, that button will also receive the motion event and there is no way to stop it.</p> <p>Also make sure you add the <code>SYSTEM_ALERT_WINDOW</code> permission to the mainifest file.</p> <p>I've posted the complete solution here:<br> <a href="http://www.jawsware.mobi/code_OverlayView/" rel="noreferrer">http://www.jawsware.mobi/code_OverlayView/</a> <strong><em>(UPDATED)</em></strong></p> <p>It includes all the source code and a link to download the entire example project.</p> <hr> <p><strong>Update for Android 4.0 - 1/3/2013</strong></p> <hr> <p>To create an overlay view, when setting up the LayoutParams <strong>DON'T</strong> set the type to <code>TYPE_SYSTEM_OVERLAY</code>.</p> <p>Instead set it to <code>TYPE_PHONE</code>.</p> <p>Use the following flags:</p> <p><code>FLAG_NOT_TOUCH_MODAL</code></p> <p><code>FLAG_WATCH_OUTSIDE_TOUCH</code></p> <p><code>FLAG_NOT_TOUCH_MODAL</code> &lt;&lt; I found this one to be quite important. Without it, focus is given to the overlay and soft-key (home, menu, etc.) presses are not passed to the activity below.</p> <p><strong><em>Also, the previous link (above) has been updated to reflect this update.</em></strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload