5

I am trying to achieve a floating draggable view that will be displayed across all the activities of a single app.
Meaning there will be a view of 30% height and 50% width of the screen that the user will be able to drag on the screen and it will be drawn above any activity in the app. When the user will switch to another activity within the same app the floating view should remain in the same place.

I know it is possible using SYSTEM_ALERT_WINDOW permission, which I want to avoid because this view is only required to be displayed within the app and I don't want the users to be asked to approve such permission.

Another important point:
This view is going to be part of an SDK.
Meaning that I am not responsible for the activities/fragments/layouts of the app.
I can have/assume:

  • Base Application that the hosting app will subclass
  • All the root layouts have some identifier and are of some specific type (e.g. all the root layouts are RelativeLayout and have an id root_layout)

Similar questions that was asked 4-6 years ago with 0 satisfying answers:

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Michael Kessler
  • 14,245
  • 13
  • 50
  • 64
  • 1
    Instruct the developer to call your sdk with the activity AFTER they already called the setContent and add your view to the r.id.content layout, you can get smarter and add a global layout changes to check if another view came above yours and move yours up again. – TacB0sS Jul 18 '18 at 15:36

1 Answers1

0

i can suggest you using a service running in the background holding that specific view. The use of a service will answer your need in terms where you want that specific view to remain operative until the user should choose to disable or close it manually. You need to use the asynchronous service which is the IntentService so it will not affect you ui main thread (don't forget the activitiy's life cycle and hierarchy).

see the following links please. 1 - link to google developers https://developer.android.com/training/run-background-service/create-service

2 - link to an example of such implementation https://medium.com/exploring-code/create-chat-heads-like-facebook-messenger-32f7f1a62064

Eyal Israel
  • 257
  • 4
  • 9
  • Thank you. The problem with this solution is that it uses `SYSTEM_ALERT_WINDOW` permission which I want to avoid. No app developer will want to integrate this SDK if it requires such a heavy permission... Also many users will not grant it IMO. – Michael Kessler Jul 18 '18 at 12:24
  • You don't need permissions to use services in the Android. Especially when you define in the manifest exported to false, then no other app in a given phone can access it and/or use it. – Eyal Israel Jul 18 '18 at 12:33
  • From the link you posted: "Step-1: Add android.permission.SYSTEM_ALERT_WINDOW permission to the AndroidManifest.xml file. This permission allows an app to create windows , shown on top of all other apps." – Michael Kessler Jul 18 '18 at 13:41
  • @MichaelKessler So did you get answer for this question. Without having to take SYSTEM_ALERT_WINDOW permission? – androidbash May 13 '20 at 13:59
  • 3
    @androidbash I ended up using ActivityLifecycleCallbacks and adding my view as a subview of each new activity's decor view, while preserving the position of the view manually (locating it inside a new activity at the same location where it was in the previous one). – Michael Kessler May 17 '20 at 09:29