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>