简体   繁体   中英

Modern Android Widget Development

It's 2020, and Widgets are once again all the rage after iOS finally added support for them. However, Android Widgets don't seem to have been updated since 2012.

From the Android docs :

A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:

  • FrameLayout
  • LinearLayout
  • RelativeLayout
  • GridLayout

Which means, no ConstraintLayout, no RecyclerView, no AndroidX. I don't want to go back to the dark days of ListView.

Is the documentation on the Android developer site really still the best practice for developing Android Widgets today? Does anyone have experience with making Widgets in an AndroidX world? Is there any kind of 3rd party API to make the experience less painful?

However, Android Widgets don't seem to have been updated since 2012.

Correct.

I don't want to go back to the dark days of ListView.

Ummm... sorry?

Is the documentation on the Android developer site really still the best practice for developing Android Widgets today?

App widgets are less about "best practices" and more about the documented and supported protocol. Bear in mind that app widgets really are an inter-process communication (IPC) protocol that just happens to result in a UI in the end. Your app is not rendering the UI — the home screen or other app widget host is, usually with the assistance of AppWidgetHost and related APIs.

Which means, no ConstraintLayout, no RecyclerView, no AndroidX.

Correct. After all, you do not know whether the home screen has access to any of those things, or what version they are on, etc.

Is there any kind of 3rd party API to make the experience less painful?

That's not really possible. Your app has very little to do with app widgets. The "heavy lifting" is in the framework and, to a lesser extent, the home screen. A third-party API cannot really change that.

I was expecting Google to start steering launcher developers to start supporting slices as an alternative form of "app widget". That has not happened yet, as Google seems to have forgotten about slices. Perhaps, in the future, Google will offer something based on Android 11's SurfaceControlViewHost , which opens the doors to all sorts of possibilities. In both cases, it may require launcher developers to make changes to support these things, which will slow adoption.

The closest thing that you can do to offer a UI beyond what is in the limited list of widgets and containers is to render your own stuff to a Bitmap and show that in an ImageView in the app widget. This gets tricky, as you do not know the precise size of the app widget. And, of course, that does not change the input options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM