Multiple widget sizes

China☆狼群 提交于 2019-12-03 17:24:59

I had the same need with my widget. Precisely those two links you have posted helped me find the solution, which came at last with a bit more of research.

In order to have a second size for your widget:

  1. Define another receiver in your manifest, with a diferent name. This is important: if you don't use a different name, only one will show in the list of available widgets. For example: android:name=".MyWidget_Small" and android:name=".MyWidget_Medium". Also modify the labels accordingly

  2. The additional receiver needs a new appwidget provider in res/xml. In android:resource of the meta-data tag type the name of another XML file. You'll have then android:resource="@xml/MyWidgetProvider_Small" and android:resource="@xml/MyWidgetProvider_Medium"

  3. Each XML file in res/xml must point to a separate layout. You will have android:initialLayout="@layout/MyWidget_Small" and android:initialLayout="@layout/MyWidget_Medium"

  4. Now the Java part. Each appwidget provider need its correspondent Java class that extends AppWidgetProvider, so you will need two additional classes. If your original class was MyWidget.java, you'll add MyWidgetSmall.java and MyWidgetMedium.java. You'll probably end up subclassing your original Java class, and perhaps modifing each new class if the widget has a distinct behavior in each size. Remember to name the two classes as you did in step 1 in the receivers's android:name

  5. Your service doesn't need duplication. However you should examine your source code to find occurrences of explicit references to MyWidget.class. If you did this, you must reference the actual subclass

Hope this helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!