First instance of AppWidget doesn't respond, but additional instances do?

I'm an experienced J2ME developer, but new to Android. I've attempted to create an AppWidget and I'm having a problem. I've "boiled-down" my problem into a simple example called TestWidget.

[Please note that I would have included some small and simple screen shots, but as a new user I'm not allowed to include images]

Here's what's happening: after I download my app to an emulator (2.3.3), I long-press on the home screen, select Widgets and then choose my own widget (called TestWidget). My TestWidget then appears on the home screen. It looks like this:

------- | | | Unset | | | -------

The layout for the widget is contained in:


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:background="@drawable/gradient_rect_yellow" android:orientation="vertical" android:layout_width="72dp" android:layout_height="72dp" android:layout_margin="8dp" > <TextView android:id="@+id/widget_text" style="@android:style/TextAppearance.Medium" android:text="Unset" android:textColor="#000" android:layout_margin="4dp" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>

(Please notice that the TextView is initialized to contain the word "UNSET" by:


Here is the code for that sets up the widget:

package com.unme2.testwidget; import; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.RemoteViews; public class WidgetProvider extends AppWidgetProvider { static private int count_ = 0; @Override public void onUpdate (Context context, AppWidgetManager manager, int ids[]) { int id, numids, slot; Intent intent; PendingIntent pending; RemoteViews views; super.onUpdate(context, manager, ids); views = new RemoteViews (context.getPackageName(), R.layout.testwidget); views.setTextViewText (, ++count_ + ""); intent = new Intent (context, TestActivity.class); pending = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); views.setOnClickPendingIntent (, pending); numids = ids.length; for (slot = 0; slot < numids; slot++) { id = ids[slot]; manager.updateAppWidget (id, views); } } }

Please notice that the onUpdate method attempts two tasks. First, it arranges for a "click" on the widget to invoke and Activity called TestActivity. Second, it overwrites the text in the TextView with a number. The original text, recall, is "Unset," and the number that gets written is a count (starting with 1) of the number of times the onUpdate methods has been invoked.

As mentioned, the first instance of the App Widget placed on the home screen does not respond to "clicks," and the default text (Unset) does not get replaced with a number (which should be 1).

However, if I add a second copy of the App Widget to my home screen, it looks like this,

------- | | | 2 | | | -------

and when "clicked" it properly presents an Activity screen:

[image of Activity with only a title]

Additional App Widgets placed on the home screen also work correctly. Any advice will be greatly appreciated. Below are the rest of the files in this example:


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:orientation="vertical" android:background="@drawable/gradient_rect_yellow" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:text="TestActivity" android:textColor="#000" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout

package com.unme2.testwidget; import; import android.os.Bundle; public class TestActivity extends Activity { @Override public void onCreate(Bundle bundle) { super.onCreate (bundle); setContentView (R.layout.testactivity); } }

Finally, here is the manifest:


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="" package="com.unme2.testwidget" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="TestWidget"> <receiver android:name=".WidgetProvider" android:label="TestWidget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" /> </receiver> <activity android:name=".TestActivity" android:label="TestWidget"> </activity> </application> </manifest>

[End of post]

Category:android Views:0 Time:2011-08-30

Related post

Copyright (C), All Rights Reserved.

processed in 0.083 (s). 11 q(s)