tags:

views:

1738

answers:

3

Hi

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

+1  A: 

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.

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

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:

http://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/TwitterWidget/

bigtony
A: 

Hi bigtony, Me new to android ,actually i have a application and now my concern is 1) How do i create a home screen widget(With just a icon and a text view under it( 2) And when the widget on the home screen is clicked how do i show up my application would be looking forward for your help Thank you

warrior
HelloTry downloading the Twitter widget source code - I suggest you get this working and then try to modify it to suit your circumstances.http://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/TwitterWidget/Having learnt this from scratch myself, I can definitely recommend looking at this demo widget app - it sounds very similar to what you want to do.
bigtony