https://kotlinlang.org logo
#compose-android
Title
# compose-android
c

Colton Idle

03/17/2024, 9:48 PM
I'm in an android foreground service for my own little proof of concept app. I'm trying to draw over other apps (I have that permission granted as well) Doing this crashes
Copy code
overlayView = ComposeView(context).apply {
  setContent {
    Text("Overlay Text", Modifier.padding(16.dp))
  }
}
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.addView(overlayView, params)
with
ViewTreeLifecycleOwner not found
but if I do the same code from View land then no crash. (view system code inside 🧵 ) let me know if compose just has some weird restrictions where it can't draw like that.
If I just swap it out with the view based code then no crash. something like the below works
Copy code
overlayView = LinearLayout(context).apply {
  orientation = LinearLayout.VERTICAL
  gravity = Gravity.CENTER
  addView(textView)
}
b

Ben Trengrove [G]

03/17/2024, 10:12 PM
Because you are running outside of a ComponentActivity there are a bunch of system hooks you would have to hook up manually
I bet if you do some googling you'll find a code snippet that gets it working, it should be possible but just extra work
b

brandonmcansh

03/17/2024, 10:16 PM
Something to this effect https://stackoverflow.com/a/78102901
c

Colton Idle

03/17/2024, 10:23 PM
very interesting! let me give this a shot later today.
m

Mark Murphy

03/17/2024, 10:38 PM
c

Colton Idle

03/18/2024, 1:01 AM
nice! Didn't know this many people were hacking around with overlay stuff
b

brandonmcansh

03/18/2024, 1:03 AM
lol 😂 I did something for Gabb in 21 I think. Don’t have code to it anymore though :/
c

Colton Idle

03/18/2024, 1:08 AM
Gabb?
b

brandonmcansh

03/18/2024, 1:31 AM
Built their messenger app (for all devices) and dialer for the new device that shipped end of last year
c

Colton Idle

03/18/2024, 1:38 AM
oh that looks really cool!
b

brandonmcansh

03/18/2024, 1:38 AM
For sure. Was a fun project
2 Views