0

Context:

My android library requires to dynamically place views on on top of other target views. Currently I am using WindowManager for this purpose. I have the viewId of the target views and get the location coordinates of the views. Now I simply use windowManager.addView(). Issue faced here is that WindowManager requires SYSTEM_ALERT_WINDOW permission which we want to avoid.

I have also tried getWindow().getDecorView().findViewById(android.R.id.content).addView(imageView), so that I donot have to use WindowManger but it is causing alignment issues for different phones. Any help on how we could avoid using WindowManager and still fulfill our purpose at the same time?

Alternative solution: I recently read this stackoverflow answer which said that permission won't be asked if installed from playstore. So I launched my app on playstore but it still asks for permission during runtime. I have also filled this form.

Edit:

  1. Using ViewOverlay as suggested by Mike. Issue is that drawables can only be added upto the region where associated View extends. Image
  2. Using getWindow().getDecorView().findViewById(android.R.id.content).addView(imageView) Issue is that we get different alignment for different screens. Screen 1 Screen 2
  3. Using WindowManager. Require SYSTEM_ALERT_WINDOW permission which we want to avoid.
Girish Garg
  • 58
  • 1
  • 9
  • What's your `minSdkVersion`? Starting with API level 18, every `View` has an overlay that you can add `Drawable`s in to be drawn on top of that `View`'s content. `ViewGroup`'s overlay also allows other `View`s to be added to it, so you could add your `View`s to the main content's overlay instead; e.g., `ViewGroup content = ...findViewById(android.R.id.content);`, `content.getOverlay().add(imageView);`. You'd have to manually position the `View`s yourself, IIRC, but it seems like you might already be mostly set up to do that, since you were figuring screen coordinates anyway. – Mike M. Jun 29 '21 at 13:05
  • @MikeM. minSdkVersion is 22. I have already tried adding `Drawable` to `ViewGroup` and manually set its coordinates, but there have been alignment issues for different phones. Can you elaborate on the overlay for `Views`? – Girish Garg Jun 29 '21 at 13:39
  • I think I initially misunderstood what you mean by "alignment issues", and the overlay suggestion may end up giving you the same results, if I'm understanding correctly now. If so, then you might consider focusing on fixing those issues, because they will likely affect any solution that moves your `View`s from `WindowManager` into an `Activity`. Just a thought. – Mike M. Jun 30 '21 at 12:43
  • @MikeM. Please check edit – Girish Garg Jun 30 '21 at 14:53
  • I tried to add `Drawable` over `android.R.id.content`'s overlay but I cannot see any change even after trying different bounds. Can you take at look at this? [MainActivity.java](https://github.com/gargVader/ViewOverlay/blob/main/app/src/main/java/com/example/viewoverlay/MainActivity.java) – Girish Garg Jul 01 '21 at 06:34
  • 1
    Here ya go: https://drive.google.com/file/d/1lq11refZ2Ekbwv6CwhJ2wLqbpLUi8QZq/view?usp=sharing. Hopefully that's a little more clear. I realize now that I tried to cram too much information into my first comment, and just confused things. My apologies. Anyhoo, here's a screenshot of what that quick demo should at least resemble: https://i.stack.imgur.com/CBtJ7.jpg. – Mike M. Jul 01 '21 at 07:21
  • That was very nice of you. Can we do the same thing for `View`s too as was my original question? [MainActivity.java](https://github.com/gargVader/ViewOverlay/blob/main/app/src/main/java/com/example/viewoverlay/MainActivity.java) – Girish Garg Jul 01 '21 at 07:57
  • 1
    Yeah, but I forgot that they would have to be measured and laid out first, and since these `View`s are newly created on demand, we'll have to do it ourselves: https://drive.google.com/file/d/1UrqcVEFHbKgSTAesUUphMCV4C85oAjcG/view?usp=sharing. Result: https://i.stack.imgur.com/zRM6P.jpg. – Mike M. Jul 01 '21 at 08:43
  • Everything works well in your code except that `onClickListener` for the `View` doesnot work as expected. [MainActivity.java](https://github.com/gargVader/ViewOverlay/blob/main/app/src/main/java/com/example/viewoverlay/MainActivity.java) – Girish Garg Jul 01 '21 at 13:09
  • Mmm, no, overlay `View`s do not process touch events. It was not clear from the question that you're doing anything other than visual decoration with these. – Mike M. Jul 01 '21 at 17:10
  • Is there any other way that we can process touch events for the `View`s that are dynamically added on top of other `View`s? We are building an android library and if possible we would like to connect with you and discuss some queries regarding our product. Your expertise would definitely help us. [Chat Room](https://y99.in/rw/332135). My [LinkedIn](https://www.linkedin.com/in/girish23/) – Girish Garg Jul 02 '21 at 07:44
  • 1
    Here's that example I mentioned earlier, if it's still needed: https://drive.google.com/file/d/1ZyrWUiZHs50_plfjITubd2KUhh5jrt3c/view?usp=sharing. The code's a bit different, and it now shows when clicking the `Button` instead of at startup, but it still uses the same layout. It results in exactly the same image as that from the last example. – Mike M. Jul 02 '21 at 20:40
  • Thank you very much Mike. That works perfectly. I just set clickable to be false. I shall tag you again in some question if needed. – Girish Garg Jul 04 '21 at 07:23
  • @MikeM. After adding the views, when I scroll the screen, the target views also move. But the pointers that we have added donot move along with them as expected. Any approach that we could use to solve it? – Girish Garg Jul 15 '21 at 05:29

0 Answers0