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.
Select Apps and widgets that you want to add to the home screen. Make sure you switch to Widgets on the screen that is opening up. Find the Multicon widgets and select the one you want to use by holding down your finger on it and moving it to the location you want it to be displayed.
To create multiple widgets, click the Settings icon in the far left menu bar. In the Integrations (Legacy) section, locate the Support Widget app and click Settings. To add a new widget, click the Create Widget button in the top right of the page.
If you have two widgets on a page, only one can be opened with the default trigger icon. The other needs to be opened from a custom link/trigger you set up. This is also true if you're using the UserVoice and Classic Widget on the same page. One of them must use a custom link/trigger to open.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With