How to add multiple widgets in the same app?
I've just finished my Android widget. Now I need to have different sizes of this widget for the user to choose from.
For example, I need a medium, small and large size widget, so when the user installs the app and hold the home screen then choose widget, in the widget menu I want him to see three widgets with the same 开发者_开发知识库app name but with the size. Something like this:
helloSmall helloMedium helloLarge
I have the medium one ready, but how can I add the small and the large in the same app? Knowing that all three sizes contain the same exact data and actions, just the size and the background are different.
You need a receiver definition for each type in your manifest file like:
<receiver android:name=".MyWidget" android:label="@string/medium_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/medium_widget_provider" />
</receiver>
<receiver android:name=".MyWidget" android:label="@string/large_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/large_widget_provider" />
</receiver>
This would allow you to have the same AppWidgetProvider
class be used for multiple widgets, with different widget names and different sizes defined in the <appwidget-provider>
XML.
Now if you need more differences in your widgets than what is in the <appwidget-provider>
XML I would create a base widget class that implements all the common behavoir between the different types:
public abstract class MyBaseWidget extends AppWidgetProvider
And then each of your concrete implementations could extend MyBaseWidget. Then in your manifest file you would have a receiver definition for each of your concrete implementations like:
<receiver android:name=".MyMediumWidget" android:label="@string/medium_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/medium_widget_provider" />
</receiver>
<receiver android:name=".MyLargeWidget" android:label="@string/large_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/large_widget_provider" />
</receiver>
Actually, android:name for each widget have to be different. If you will do this as in example, only one widget will be visible in widgets list.
Guys, I had the same problem.
You need to actually add a second widget provider aswell;
<receiver android:name=**".MyWidget**" android:label="@string/medium_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/medium_widget_provider" />
</receiver>
<receiver android:name=**".MyWidget2"** android:label="@string/large_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/large_widget_provider" />
</receiver>
Enjoy
Ok so basically you will need:
layout file fore each widget. ex: main_small.xml, main_medium.xml ...
in the xml directory add a provider for each widget. ex: small_provider.xml, medium_provider.xml ... and so on (note if you don't have an xml directory add it under the drawable directory).
now what!
define a receiver in the manifest for each widget. (just like the example in the main answer)
you can use the same layout or deferent layout. basically this is up to you.
in your provider you should have something like this:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="146dip" android:minHeight="138dip" android:updatePeriodMillis="10000" android:initialLayout="@layout/main" />
make sure, for each provider to specify the target layout file you want to use. in this code I'm asking for the file main.xml in the layout directory. for my medium widget for example i'll have another provider with the same exact code but i'll change the last line
> android:initialLayout="@layout/medium".
I hope this helps if not let me know and I can upload a working example on my website and you can take a closer look at it. please let me know how it goes.
best of luck.
Some extra info to the other answers...
If you are duplicating the files mentioned, and if your widget uses a
Service
to provide some functionality, you might have to duplicate your service.If you duplicate your
Service
, remember to update your manifest with the new service, otherwise the new service won't run...
This wasted some time for me.
If you use any BroadcastReceiver
to send Intent
s to your duplicate Service
s... don't forget to update that code too:
- you must now send intents to each of the services.
精彩评论