How can I interact with elements behind a translucent Android app?

Viewed 7099

I have found how to create a translucent background for my android app, but so far I haven't found how to interact with what's behind it (the home screen for example).

This post helped me make the app view translucent.

How would one go about allowing the user to interact with what's behind the translucent app? This app is a good example of an app that allows this is the "Transparent Screen" Android app.

3 Answers

I got the answer. Adding line getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); in onCreate in second top activity(which is transparent did the trick).

Above solution has a problem. After using above whole window passes the touch to background. So if you want full control then better extend a layout e.g. Framelayout and override 'onTouchEvent' and get the touch location using event.getX() and event.getY() methods and return false where you want to pass the touch event. It will be passed to parent view.

FLAG_NOT_TOUCH_MODAL will do what you want with the least code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    ...
    setContentView(R.layout.my_activity_view);
    ...
}

public static final int FLAG_NOT_TOUCH_MODAL

Window flag: Even when this window is focusable (its {@link FLAG_NOT_FOCUSABLE is not set), allow any pointer events outside of the window to be sent to the windows behind it. Otherwise it will consume all pointer events itself, regardless of whether they are inside of the window.

Related