Making Your Widgets Interactive

You can also add interactivity to your widgets using Remote Views, but reactions to user input are tightly restricted.

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit to purchase your own copy.

Because they run within the home-screen process, the widgets themselves inherit its permissions. As a result of these security implications widget interactivity is carefully controlled.

Widget interaction is generally limited to two possibilities:

> Adding a click listener to one or more views within the layout

> Changing the UI based on selection changes

It's notable that there is no supported technique for entering text directly into an App Widget.

If you need text input from your widget, best practice is to add a click listener that displays an Activity to accept the user data when a portion of the widget is clicked.

One popular alternative is to use Image Views designed to look like Edit Text controls. By means of Selection State Drawables they can appear to gain focus. When the Image View is clicked, a partially transparent Activity is launched to accept the user input.

Using a Click Listener

The most powerful technique for adding interactivity to your widget is through the use of the setOnClickPendingIntent method on a Remote Views object.

This lets you specify a Pending Intent that will be fired when the user clicks on the specified widget View. Pending Intents (described in more detail in Chapter 5) can contain Intents used to start Activities or Services or broadcast Intents.

Listing 10-10 demonstrates a broadcast Intent assigned to a Text View element within a widget layout:

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment