https://kotlinlang.org logo
#android
Title
# android
r

ritesh

07/30/2020, 8:12 AM
Hi Everyone, Wondering if any know or worked on any alternative to 
SYSTEM_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.
Copy code
<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.
k

KamilH

07/30/2020, 8:29 AM
Hi, I recently implemented something like that with use of
Application.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 manually
For now I ditched both options and I stayed with the not looking good animation, but in near future I will eventually fix it with the second solution
r

ritesh

07/30/2020, 8:48 AM
Thanks a lot @KamilH Let me try out this approach. I will circle back, if i face any issue or able to fix the issue which you faced.
k

KamilH

07/30/2020, 8:52 AM
Let me know if I can help you and good luck 🙂
r

ritesh

07/30/2020, 8:54 AM
Sure. Thanks!