ritesh
07/30/2020, 8:12 AMSYSTEM_ALERT_WINDOW
to display an overlay across all activities.
Why not use SYSTEM_ALERT_WINDOW
and Services like every other stackoverflow solution tells me to do that.
Requirement is to add a chat feature in the app, that would be available across all activities in minimized view once initiated - you can start a chat within one activity and keep it live when changing activities.
Solution -> Use this permission, and create a service, to overlay across all screens.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Problem with the requirement we have ->
1. I don't need a view visible over all applications - a view visible only to our app.
2. Using the above solution requires run time permission from users for api level above >=M.
3. User can disable this permission from settings, and overlay won’t work.
4. this permission is not available on all devices, and I suspect that it will be retired in a year or two.KamilH
07/30/2020, 8:29 AMApplication.ActivityLifecycleCallbacks
.
I’m adding custom view to the Resumed Activity Window via activity.windowManager.addView(view, layoutParams)
and I’m removing it from Stopped Activity via activity.windowManager.removeView(view)
Every time user drags the view I’m updating it with activity.windowManager.updateViewLayout(view, layoutParams)
. It works pretty well
There are few problems to solve: I’m using setOnTouchListener
on the draggable view to move it around so click events needs to be handled differently.
My biggest issue is that there is a default Window animation that is trying to animate window size changes and it doesn’t look good in my case (I have an expand/collapse feature of the draggable view). The animation is possible to disable only via reflection:
https://stackoverflow.com/a/33171254/3448282
or add custom container (FrameLayout for example) to the WindowManager instead of adding draggable view itself and handle everything manuallyritesh
07/30/2020, 8:48 AMKamilH
07/30/2020, 8:52 AMritesh
07/30/2020, 8:54 AM