How to implement a Button on an Android Widget
-
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
-
Answer:
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
bigtony at Stack Overflow Visit the source
Other answers
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
Related Q & A:
- How to implement a relative timer in a game?Best solution by Stack Overflow
- How To Click A Button In A Popup?Best solution by Stack Overflow
- How to implement a physics engine?Best solution by Game Development
- How to implement a voting system?Best solution by Stack Overflow
- How to add a button in a Fragment?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.