views:

1012

answers:

5

Hello,

I've just finished my Android widget. Now I need to have different sizes of this wiget for the user to choose from. for example I need a medium, small and large size widget. so when the user install the app and hold the the home screen then choose widget, in the widget menu I want him to see three widget 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 make 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.

Thanks.

+7  A: 

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>
mbaird
Thanks a lot that makes since!! I was very close. I'll work on it and I come back eather with a success message or more questions.Thanks a lot.
BarcaDroid
Hi, thanks for your answer. But it seems that I cannot share the same widget provider. (if I do, only first in manifest is shown). So, basically, multiple widget providers would be the answer for me. Is anyone working with one widget provider?
xandy
Yep, just followed above instructions and found it only works if i specify different widget provider classes. Good anyway, thanks everyone! :)
dpimka
Same here, need multiple widget provider classes for widgets to show up.
chakrit
A: 

ok so I did your first solution but still I get only one widget. I don't know what's wrong?

now I have two receivers each is calling a separate widger_provider.xml .

but still I only see the first one when I run the app in the widget menu.

This is not an answer but there is no option to write a comment after i registered. so, I'm sorry.
You should have added a comment to my answer, or edited your original question. Anyway, can you post your Manifest.xml?
mbaird
Thanks a lot I figured it out it was an error on my side. I guess I didn't look at the code you provided carefully. I got it to work just fine now. again Thank you so much.
A: 

I bump this topic because I tried this solution and I still get only one widget on the widget list

How did you solve your problem Barcadroid please ?

+1  A: 

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.

Hi John, I'm sorry for the delay but I didn't get your question notification early on my email. Anyway, I'll upload a whole sample project on my website and post the link in here soon. Hang in there Buddy!! I know your frustration... just give me some time because I'm working on other stuff as well...
A: 

I followed the instructions, the app compiles but i still see only one widget. Could somebody share please? barcadroid?

I guess not.

John