January 27, 2021
Hot Topics:

Handling User Interaction with Android App Widgets, Page 2

  • By Lauren Darcey, Shane Conder
  • Send Email »
  • More Articles »

Handling the State

Now that you understand how the user triggers an Intent object to be passed to the App Widget and how it's caught, you can handle the requested operation. This involves:


  1. Getting the appropriate RemoteViews instance
  2. Retrieving and changing its state
  3. Applying these changes to the RemoteViews control
  4. Calling the updateAppWidget() method to apply the changes to the appropriate App Widget.


The code for handling each of these tasks is found in the implementations of the onHandleAction() and updateDisplayState() methods in the source code available for download with this article.

Each time the RemoteViews control is updated, it is re- created from the original layout. Now, however, click handlers are used. Based on the current state of the App Widget, the required click handlers are added in the same way as we added the configuration Activity action earlier. These handlers are also available for review within the updateDisplayState() method of the source code.

Updating the Layout

To achieve the effect of the button bar drawing over the bottom of the widget, we employ two FrameLayout controls. The outer FrameLayout--comprising the entire App Widget-- contains the ImageView to display and another FrameLayout for the button bar. This second FrameLayout is set to draw at the bottom of its parent and it contains the three ImageButton controls for Settings, Play/Pause, and Next Image. The button bar FrameLayout can be hidden from view by simply setting its visibility attribute to GONE. The complete layout file (widget.xml) can be viewed in the sample code provided with this article.

Updating the Android Manifest File

Finally, the system won't know that this can handle our new action type unless we update the AndroidManifest.xml file to include the appropriate intent filter:


  <receiver android:name=".ImagesWidgetProvider">
        <action android:name=
           "com.mamlambo.ImagesWidget.WIDGET_CONTROL" />
        <data android:scheme="images_widget" />


This intent filter instructs the application to watch for the broadcast Intents with the action type of ACTION_WIDGET_CONTROL, whose value is "com.mamlambo.ImagesWidget.WIDGET_CONTROL".


App Widgets add a whole new dimension to the Android user experience. These App Widgets needn't be static, but can contain interactive features and react to user events like clicks. In this article, you learned how to add user interaction to an Android App Widget. In doing so, you learned how to interact with RemoteViews using a PendingIntent. Finally, you learned about the limitations of the Intent mechanism and how to work you can around them to create a App Widget users can control without leaving the comfort of their Home Screen.

Read part 1: Creating a Home Screen App Widget on Android here.

About the Authors

Shane Conder is a software developer focused on mobile and web technologies. He is currently working at a small mobile software company. With almost two decades of experience in software production, Lauren Darcey specializes in the development of commercial grade mobile applications. Recently, Shane and Lauren coauthored an in-depth programming book entitled Android Wireless Application Development, available from Addison-Wesley (ISBN: 0321627091). They can be reached at androidwirelessdev+a4@gmail.com and via their blog at http://androidbook .blogspot.com.

Originally published on https://www.developer.com.

Page 2 of 2

This article was originally published on September 8, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date