Android draw unclickable layout over apps

582 Views Asked by At

I have a layout that I draw over other apps which is set to match_parent to fill the whole screen. In this layout I have a chat head which can be dragged around and clicked to reveal more. Normally I would set the layout to wrap_content and take up only the screen space I need, but the Android SpringAnimations aren't working without a large parent to throw my chat head around in.

The problem I'm having is I cannot interact with the background that I've drawn my layout on top of. I've previously tried setting the RelativeLayout root_container to clickable: false and focusable: false in XML and programmatically, but I still can't click anything else. Is there a solution where my root_container won't register clicks, but my chat head within it can continue to?

I sense this question is different than the other unfocusable/unclickable questions in that it involves "draw over other apps" functionality which could change things I'm unaware of.

<!--Root container - Causing the problems with elements beneath it-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--Small Chat head view that can be dragged around root_container-->
    <RelativeLayout
        android:id="@+id/collapse_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:visibility="visible">

        <ImageView
            android:id="@+id/floating_icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/floating_icon"
            tools:ignore="ContentDescription" />

        <ImageView
            android:id="@+id/close_button"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="40dp"
            android:src="@drawable/ic_close"
            tools:ignore="ContentDescription" />
    </RelativeLayout>
</RelativeLayout>

Edit: I call my layout in a service like this:

mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_view, null);

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);

Thanks for taking a look!

1

There are 1 best solutions below

0
On

With some research, I found that this is not possible due to various malicious opportunities it could allow. If the view is made FLAG_NOT_TOUCHABLE then it cannot be interacted with, and if you try to dispatch touch events, they will not go to the home screen. The solution is simply to refactor so the parent view does not fill the home screen.