How to add multiple widgets in the same app?

AndroidAndroid Widget

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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>

Solution 2 - Android

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.

Solution 3 - Android

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

Solution 4 - Android

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:

> >
> 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.

Solution 5 - Android

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 Intents to your duplicate Services... don't forget to update that code too:

  • you must now send intents to each of the services.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBarcaDroidView Question on Stackoverflow
Solution 1 - AndroidMark BView Answer on Stackoverflow
Solution 2 - AndroidkorroView Answer on Stackoverflow
Solution 3 - AndroidjblzView Answer on Stackoverflow
Solution 4 - Androiduser308238View Answer on Stackoverflow
Solution 5 - AndroidRichard Le MesurierView Answer on Stackoverflow