24

I am just getting started with Android development and I have created a nice little widget that displays some info on my home screen. However, I now want to implement a Button on my widget that updates the info in my widget TextView.

Can anyone advise how to go about doing this?

Thanks

bigtony
  • 817
  • 1
  • 6
  • 11
  • 3
    I have read the developer docs - which is how I managed to create the widget! But can anyone advise how a button is implemented on a widget - ie. I have not got an Activity on which to set up an setOnClickListener. So I wonder how Buttons are implemented with a widget. – bigtony Jan 17 '10 at 23:04
  • http://developer.android.com/reference/android/widget/Button.html – YuppieNetworking Jan 17 '10 at 23:31
  • 4
    Yes I have seen that article, but it only shows how to use a button in an Activity, yet I have no Activity in my widget. Perhaps an Activity is needed, but I am not sure how to connect the activity with the widget, and this is the basis of my question - how do I go about implementing a Button in an android Widget. – bigtony Jan 18 '10 at 00:00

2 Answers2

24

Solved - I can confirm that an Activity is NOT needed if you want create a Button to update an Android AppWidget.

I have been able to implement my AppWidgetProvider class such that it registers an android.appwidget.action.APPWIDGET_UPDATE intent-filter with the Broadcast receiver in the AndroidManifest.xml, which then fires the onUpdate event in the AppWidgetProvider class (which in turn then runs the UpdateService).

<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".MyWidget" android:label="@string/widget_name">
   <intent-filter>
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
   </intent-filter>
   <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>

The UpdateService in my AppWidgetProvider class then uses onHandleIntent to run a private buildUpdate method - which registers the onClick event with a call to setOnClickPendingIntent as follows:

// set intent and register onclick
Intent i = new Intent(this, MyWidget.class);
PendingIntent pi = PendingIntent.getBroadcast(context,0, i,0);
updateViews.setOnClickPendingIntent(R.id.update_button,pi);

Here is a link to some source code of a working example, which shows how an update button can be used to update a Twitter widget:

https://github.com/commonsguy/cw-advandroid/tree/b01438e7f0fed8f795ddec4be43066905f03d0cc/AppWidget/TwitterWidget

ZeroOne
  • 3,041
  • 3
  • 31
  • 52
bigtony
  • 817
  • 1
  • 6
  • 11
4

Button is supported in appwidget so not sure what the problem is. Look at this example on how assign actions via views.setOnClickPendingIntent(R.id.YOURVIEW ID, yourIntent);

A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:

FrameLayout LinearLayout RelativeLayout

And the following widget classes:

AnalogClock Button Chronometer ImageButton ImageView ProgressBar TextView Descendants of these classes are not supported.

Community
  • 1
  • 1
Alex Volovoy
  • 67,778
  • 13
  • 73
  • 54
  • Hi - thanks, I do understand how to use views.setOnClickPendingIntent in my AppWidget onUpdate() method, but I am not sure how to implement the Activity (do I need one? I assume so - but is it implemented like a normal Button?) and how to set up my Manifest file to register the Intent with the Activity (what is the action?). – bigtony Jan 18 '10 at 08:53
  • It depends on what you're trying to do. If you want to show some sortof dialog - yes, you create activity for it. Than in your button ( i assume it's part of the widget ) in views.setOnClickPendingIntent you create an intent to launch that activity. – Alex Volovoy Jan 18 '10 at 15:09
  • Thanks - Yes the button is part of a widget. Actually I don't want to create a dialog, which was why I am hesitant about using an Activity. So am I right in thinking that I don't need an Activity when I only want a button to update the widget through a service? – bigtony Jan 18 '10 at 17:11
  • Yes, create image button with good indication of refresh (), or button with text refresh. And then just handle that to your service – Alex Volovoy Jan 18 '10 at 17:26