2013-07-05

Hey everyone…….Hope all are well…..

This post is about Android Widgets.

This is going to be one of the bigger posts in Coderzheaven.

This posts helps you to handle multiple instances of a Widget.

Many of our problem is to identify different instances of the same widget.

But there is a solution to this problem.

Please check out my other posts in Coderzheaven to get an Idea of Android Widgets and it’s working.

Create new Widget

http://www.coderzheaven.com/2012/06/15/placing-controls-widget-android-listening-events-them/

How to get Notified when a widget is deleted?







Here is how we start.

First of all you need to keep these things about the Widget in Mind.

1. Every instance of Same Widget is associated with a Unique ID.

2. Widgets ID’s are sent through in the activities that has the “android.appwidget.action.APPWIDGET_CONFIGURE” intent-filter.

3. RESULT_OK should sent back to the widget

Now we will start.

First we will look at the layouts.

This is the Widget Layout.

widget_main.xml

Now the widgetprovider XML.

Please note that you have to use the exact package name with the class named you used as Configure Intent-filter in the “android:configure” property.

Now the XML for the settings Page.

Please check the above screenshot how it looks like when clicking on the widget.

It is having an edittext in which we will enter the value for the widget.

Our all layout xml are now complete.

Now we will go to the java classes.

We will write the MyAppWidgetProvider class

MyAppWidgetProvider.java

Please make sure you read the comments in this java, so I am providing any description for that.

Now the SettingsPage that edit the widget.

Now the AndroidManifest File.

Make sure you carefully read the Manifest files. Look at the intent filters in the activities and make sure that you have the correct package name in the provider xml file.

AndroidManifest.xml

Hooray you multiple instance code is complete.

Here we are saving data for each widget in the preferences with the widgetId as key with which we are reloading the data and editing it.

You can download the complete source code from here.

Link to this post!

More Similar Posts

How to get Notified when a widget is deleted?

Placing Controls in a Widget in Android and Listening to the

How to send Data between two individual applications in…

How to create a widget in android?

Passing data between Intents or classes in ANDROID?

Show more