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?

2
  • 1
    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?
    – SaadAAkash
    Aug 28, 2020 at 14:20
  • 1
    @SaadAAkash Yes I mean App Widgets that go to the home screen. They could be any of the ones you just mentioned. Aug 28, 2020 at 14:33

4 Answers 4

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.

0
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

1
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

1
  • 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! Dec 19, 2021 at 21:55
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

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. May 20, 2021 at 21:20
  • 2
    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? May 20, 2021 at 21:27
  • 2
    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. May 20, 2021 at 21:39
  • 3
    Awesome, I am very glad there's no misunderstanding! And thank you for helping me out with my questions. May 20, 2021 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.