5. ...and I ♥ homescreen widgets
Examples
Music Players
Settings controls
6. Key reasons to build a widget
• At-a-glance information
– unread messages, calendar items, to do lists
• Control apps that run in the background
– Music player
• Toggle settings
– settings that affect other applications such as GPS or wifi
– Consider how the Google Voice widget can toggle between “Use for all calls”,
“Do not use for calls”, “International calls only”, and “Ask for every call”. You
might want to toggle this setting before opening the Dailer app
• “Smart” shortcuts
– Reduce something that would normally take at least 2 steps into 1
– If it simply opens another application, keep it as a regular application shortcut
7. (Another) key reason to build a widget
Keep users engaged with your app!
out of sight == out of mind
Hey!
Don’t forget about me!
8. Designing Widgets
Widget Design Best Practices UI Guidelines
developer.android.com/guide/practices/ui_guidelines/widget_design.html
11. 1. Download
2. Import into Eclipse
as an ‘Existing Project’
3. Run
Sample Widget Code
code.google.com/p/androidto-basicwidget/
12. Overview of Widget Development
1) Create a new Android project in Eclipse
without an Activity class
2) Declare AppWidgetProviderInfo object
3) Create xml layout file for widget view
4) Extend the AppWidgetProvider class
5) Update AndroidManifest.xml
13. Overview of Widget Development
1) Create a new Android project in Eclipse
without an Activity class
14. Create a new Android project
Widgets not available in
Android 1.1
No need for an Activity
unless there’s going to be
a standalone application
18. Overview of Widget Development
3) Create xml layout file for widget view
19. XML Layout File
1. There’s already a main.xml file in the
res/layout folder so just reuse that.
20. XML Layout File
2. I renamed it to widget.xml so it was more descriptive.
3. Added a background and padding to the container.
4. And gave the TextView an id of current_time.
21. XML Layout File
5. And let’s also add a refresh button as a ImageButton
with and id of refresh.
22. If a widget is not an Activity, what is it?
An Activity could simply be
described as an UI screen.
23. If a widget is not an Activity, what is it?
An Activity could simply be
described as an UI screen.
It contains View widgets such
as LinearLayout, TextView,
and Button typically marked
up in a layout xml file.
24. If a widget is not an Activity, what is it?
An Activity could simply be
described as an UI screen.
It contains View widgets such
as LinearLayout, TextView,
and Button typically marked
up in a layout xml file.
e.g.
Button btn =
(Button) findViewById(R.id.my_button);
btn.setText(“Submit”);
25. If a widget is not an Activity, what is it?
Widgets also contain View
widgets such as LinearLayout,
TextView, and Button (but
there are limitations to what
Views you can use).
26. If a widget is not an Activity, what is it?
Button btn =
(Button) findViewById(R.id.my_button);
btn.setText(“Submit”);
27. If a widget is not an Activity, what is it?
RemoteViews
RemoteViews remoteView =
new RemoteViews(
context.getPackageName(),
R.layout.widget
);
remoteView.setTextViewText(
R.id.my_button,
“Submit”
);
28. If a widget is not an Activity, what is it?
The application component that
supplies the UI for a widget is a
BroadcastReceiver
RemoteViews
32. AppWidgetProvider
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
setAlarm(context, appWidgetId, 30000);
}