How to add multiple widgets in the same app?

Solution 1:

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:

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:

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:

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.