Can Jetpack Compose be used to build App Widgets?
Asked Answered
I

4

21

With swift, widgets can be written using SwiftUI, but I have not seen any statements regarding whether Jetpack Compose can be used to build Android App Widgets.

Is there any information where I can see how that can be done now that Compose is in Alpha?

Ido answered 28/8, 2020 at 13:58 Comment(2)
A very important question that I was pondering for a long time too, even thought about asking it here on SO. Upvoting it & also looking for good answers. Btw by Android Widgets, do you mean weather/calendar/music/clock widgets?Thrombus
@Thrombus Yes I mean App Widgets that go to the home screen. They could be any of the ones you just mentioned.Ido
P
9

You can use AndroidX's Glance, which is now an Alpha release:

https://developer.android.com/jetpack/androidx/releases/glance

You can find an example code project here:

https://github.com/android/user-interface-samples/tree/glance/AppWidget/glance-widget

Planter answered 18/12, 2021 at 20:54 Comment(1)
Going to mark this as the accepted answer as now it is the way to solve this problem with the current state of things. I hope this helps new people searching for this to arrive to the correct documentation!Ido
S
14

That depends a lot on what you mean by "Jetpack Compose". We were discussing this just yesterday on Kotlinlang Slack.

If you mean Compose UI and related packages — the library of composables that we can use to build the UIs displayed by activities — then no, this will have little role with app widgets. You could, in principle, render a composable to a Bitmap, then use that with an ImageView in an app widget, but that's about it.

If you mean Compose Runtime — the underlying "plumbing" behind @Composable, recomposition, remember(), and so on — then there may be something offered in the future. Basically, as I understand it, there might be another set of composables tied to building up and publishing a RemoteViews view hierarchy. On the plus side, this will give your AppWidgetProvider a Compose "feel". However, it will not cause something like TextField to start working magically in an app widget.

Samuelsamuela answered 28/8, 2020 at 15:50 Comment(0)
S
13

Updated Nov 08, 2021: New Jetpack library GlanceAppWidget as mentioned in this video. Could not find more information on Glance at time of writing.

Saw this post by Google. Looking forward to more announcements! enter image description here

Link: https://developer.android.com/jetpack/androidx/compose-roadmap

Syne answered 29/7, 2021 at 2:33 Comment(1)
The guys here are already experiencing with the snapshot library: youtube.com/watch?v=lN3m164VV9s&t=2131sArgufy
P
9

You can use AndroidX's Glance, which is now an Alpha release:

https://developer.android.com/jetpack/androidx/releases/glance

You can find an example code project here:

https://github.com/android/user-interface-samples/tree/glance/AppWidget/glance-widget

Planter answered 18/12, 2021 at 20:54 Comment(1)
Going to mark this as the accepted answer as now it is the way to solve this problem with the current state of things. I hope this helps new people searching for this to arrive to the correct documentation!Ido
I
7

Turns out this is something that is not possible yet, but it's in the works.

According to this video at 12:16 onwards by the Android Developers YouTube channel, this is currently an idea that is being explored to be released later this year (2021).

An example of how it may look like is this (Taken directly from the same video):

Conceptual code showing how building Widgets with Compose might look like

Ido answered 20/5, 2021 at 18:56 Comment(4)
That is exactly in line with my answer (that I think you downvoted). They are going to provide a library of composables that will help you manipulate RemoteViews. They may include some amount of backwards compatibility (e.g., use native CheckBox support in RemoteViews on Android 12+ and fake it with ImageView on older devices). But they are still going to be limited by RemoteViews, as that is the underlying IPC protocol used for apps to publish app widgets. Compose cannot change that.Samuelsamuela
I wanted to provide some links so that other devs can easily navigate themselves to the video too and see what they have to say. I absolutely did not downvote your answer, I upvoted it and marked it as the accepted answer as I believe you did a great job answering actually. Sorry if I made it look that way, how else would I be able to provide this information (mainly the links and the sample code) to other curious devs without doing what I just did?Ido
My sincere apologies. There was a downvote very close in time to when you posted this answer, and I guessed incorrectly as to where the downvote came from. Having another answer is perfectly fine. If it were not for the screenshot, you also could have posted the links as a comment, but the screenshot certainly adds value. Again, I apologize for my mistake.Samuelsamuela
Awesome, I am very glad there's no misunderstanding! And thank you for helping me out with my questions.Ido

© 2022 - 2024 — McMap. All rights reserved.