This is only my second time using widgets, and first time using buttons on it… or anything other than just TextViews in general.
So basically all I have is a widget… When I click it it doesn’t do anything and it never updates. Why? Confused… 😛
So, here is my WidgetAct onUpdate code:
@Override
public void onUpdate(Context c, AppWidgetManager awm, int[] appWidgetIds){
Log.i("IN","onUpdate");
RemoteViews rv = new RemoteViews(c.getPackageName(),R.layout.widget);
Intent twitter_intent = new Intent(c,TwitterUpdate.class);
twitter_intent.setAction(ACTION_WIDGET_TWITTER);
Intent fb_intent = new Intent(c,FBUpdate.class);
fb_intent.setAction(ACTION_WIDGET_FB);
//Intent curr = new Intent(c,WidgetAct.class);
//curr.setAction(ACTION_WIDGET_WA);
PendingIntent twitter_pi = PendingIntent.getActivity(c, 0, twitter_intent, 0);
PendingIntent fb_pi = PendingIntent.getActivity(c,0,fb_intent,0);
rv.setOnClickPendingIntent(R.id.tweet, twitter_pi);
rv.setOnClickPendingIntent(R.id.fb, fb_pi);
awm.updateAppWidget(appWidgetIds, rv);
}
Now here is my manifest (or the important part):
<receiver android:name="WidgetAct">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.laytproducts.IN.WidgetAct.ACTION_WIDGET_FB" />
<action android:name="com.laytproducts.IN.WidgetAct.ACTION_WIDGET_TWITTER" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/winfo"/>
</receiver>
Here is my xml for the widget provider:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth = "220dp"
android:minHeight = "72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget">
Here is my layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout android:background="@drawable/inwidget" android:id="@+id/frameLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageButton android:scaleType="fitXY" android:src="@drawable/twitterlogo" android:layout_marginTop="17dp" android:id="@+id/tweet" android:layout_marginLeft="20dp" android:layout_width="40dp" android:layout_height="40dp"></ImageButton>
<ImageButton android:scaleType="fitXY" android:src="@drawable/facebooklogo" android:layout_marginLeft="5dp" android:id="@+id/fb" android:layout_toRightOf="@+id/tweet" android:layout_alignTop="@+id/tweet" android:layout_alignBottom="@+id/tweet" android:layout_width="40dp" android:layout_height="40dp"></ImageButton>
</RelativeLayout>
</FrameLayout>
Kind of a lot of code… but I literally have NO reason to believe that I broke something XD… but that is because my widget knowledge is minimal.
Hopefully its a small mistake I over looked.
Thanks all,
Brandon
According to the explanation in the SDK document, the
onUpdate()method is designed to respond to theACTION_APPWIDGET_UPDATEbroadcast, which will be sent in conditions below:In your code, you set the action
com.laytproducts.IN.WidgetAct.ACTION_WIDGET_FB, so theonUpdate()won’t be called. I haven’t usedgetActivityin widget, but I have usedgetBroadcastand it works well.I think you should handle your click event in the
onReceive()method. Please note that theAppWidgetProviderextends fromBroadcastReceiver, so onlyonReceive()will be called(in theonReceive()in the source code ofAppWidgetProvider,onUpdate()is called inside. That’s why we haveonUpdate()). You should do something like:This is what I did in my widget. Hope it helps.