25

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

3
  • 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. Commented Jan 17, 2010 at 23:04
  • developer.android.com/reference/android/widget/Button.html Commented Jan 17, 2010 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. Commented Jan 18, 2010 at 0:00

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

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?).
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.
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?
Yes, create image button with good indication of refresh (), or button with text refresh. And then just handle that to your service

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.