How to add multiple widgets in one application?

I 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 widget of medium, small and large size. therefore, when the user installs the application and holds the main screen, and then select the widget, in the widget menu I want him to see three widgets with the same application name, but with a size. something like that:

helloSmall helloMedium helloLarge

I have medium, but how can I make small and large in one application? knowing that all three sizes contain the same exact data and actions, only the size and background are different.

Thank.

+60
android android widget widget size
Apr 03 2018-10-10T00:
source share
5 answers

For each type of manifest file, you need a receiver definition, for example:

<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 will allow you to use the same AppWidgetProvider class 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 in XML <appwidget-provider> , I would create a base class of widgets that implements all the usual behavior of different types:

 public abstract class MyBaseWidget extends AppWidgetProvider 

And then each of your specific implementations can extend MyBaseWidget. Then, in your manifest file, you will have a receiver definition for each of your specific implementations, for example:

  <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> 
+96
Apr 03 '10 at 2:42
source share

Actually, android: the name for each widget should be different. If you do this, as in the example, only one widget will be visible in the list of widgets.

+26
Feb 07 '11 at 11:01
source share

Guys, I had the same problem.

You must also add a second widget provider,

 <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

+11
Apr 20 '11 at 23:00
source share

Ok, so basically you will need:

for 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 ... etc. (note that if you do not have an xml directory, add it to the drawable directory).

now what!

  • Define the recipient in the manifest for each widget . (exactly the same as in the main answer)

  • You can use the same layout or layout. basically it is up to you.

  • at 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 the destination layout file you want to use is specified for each provider. in this code, I request the main.xml file in the layout directory. for my middle widget, for example, I will have another provider with the same exact code, but I will change the last line

 > android:initialLayout="@layout/medium". 

Hope this helps if you don't let me know, and I can download a working example on my website, and you can take a closer look at it. please let me know how this happens.

Good luck.

+5
May 12 '10 at 10:43
source share

More info for other answers ...

  • If you duplicate the mentioned files, and if your widget uses Service to provide some functions, you may need to duplicate your service.

  • If you duplicate Service , remember to update the manifest with the new service , otherwise the new service will not start ...

It spent time on me.




If you use BroadcastReceiver to send an Intent to your duplicate Service s ... remember to also update this code:

  • now you have to send intentions to everyone .
+1
Jan 22 '13 at 15:08
source share



All Articles