SlideShare une entreprise Scribd logo
1  sur  37
Homescreen Widgets
Demystified
Pearl Chen
@androidsNsheep
AndroidTO // Oct 26, 2010
Hi, I'm a web developer...
So many ways to create
an Android application…
but none of them
create homescreen widgets!
...and I ♥ homescreen widgets
Examples
default Donut homescreen
default Froyo homescreen
...and I ♥ homescreen widgets
Examples
Gmail Unread Count
HTC SenseUI email widget
...and I ♥ homescreen widgets
Examples
Music Players
Settings controls
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
(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!
Designing Widgets
Widget Design Best Practices UI Guidelines
developer.android.com/guide/practices/ui_guidelines/widget_design.html
Designing Widgets
Lighter color theme
in Cupcake and Donut
Darker color theme
in Éclair and Froyo
Coding Widgets
App Widgets Framework Guide
developer.android.com/guide/topics/appwidgets/index.html
1. Download
2. Import into Eclipse
as an ‘Existing Project’
3. Run
Sample Widget Code
code.google.com/p/androidto-basicwidget/
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
Overview of Widget Development
1) Create a new Android project in Eclipse
without an Activity class
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
Overview of Widget Development
2) Declare AppWidgetProviderInfo object
AppWidgetProviderInfo
1. Create an xml folder
in the res folder.
2. In the xml folder,
create a new
Android XML File
AppWidgetProviderInfo
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis=“30000"
android:initialLayout="@layout/widget">
</appwidget-provider>
(number of cells * 74) - 2
Following this formula, you should use 72 dp for a height of one cell, 294 dp and for a width of four cells.
(number of cells * 74) - 2
Overview of Widget Development
3) Create xml layout file for widget view
XML Layout File
1. There’s already a main.xml file in the
res/layout folder so just reuse that.
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.
XML Layout File
5. And let’s also add a refresh button as a ImageButton
with and id of refresh.
If a widget is not an Activity, what is it?
An Activity could simply be
described as an UI screen.
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.
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”);
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).
If a widget is not an Activity, what is it?
Button btn =
(Button) findViewById(R.id.my_button);
btn.setText(“Submit”);
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”
);
If a widget is not an Activity, what is it?
The application component that
supplies the UI for a widget is a
BroadcastReceiver
RemoteViews
Overview of Widget Development
4) Extend the AppWidgetProvider class
AppWidgetProvider
public class BasicWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(
Context context,
AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
super.onUpdate(
context,
appWidgetManager,
appWidgetIds
);
//do some stuff every updatePeroidMillis!
}
}
AppWidgetProvider
void onUpdate( Context context,
AppWidgetManager appWidgetManager,
int[] appWidgetIds )
void onEnabled( Context context )
void onDeleted( Context context,
int[] appWidgetIds )
void onDisabled( Context context )
void onReceive( Context context,
Intent intent )
AppWidgetProvider
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
setAlarm(context, appWidgetId, 30000);
}
AppWidgetProvider
AppWidgetProvider
Overview of Widget Development
5) Update AndroidManifest.xml
AndroidManifest.xml
THANK YOU!
Pearl Chen
@androidsNsheep

Contenu connexe

Tendances

The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...Katy Slemon
 
08.1. Android How to Use Intent (explicit)
08.1. Android How to Use Intent (explicit)08.1. Android How to Use Intent (explicit)
08.1. Android How to Use Intent (explicit)Oum Saokosal
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and EventsHenry Osborne
 
Android App Development 03 : Widget &amp; UI
Android App Development 03 : Widget &amp; UIAndroid App Development 03 : Widget &amp; UI
Android App Development 03 : Widget &amp; UIAnuchit Chalothorn
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in androidPrawesh Shrestha
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycleAhsanul Karim
 
A comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter componentA comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter componentKaty Slemon
 

Tendances (10)

The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...
 
08.1. Android How to Use Intent (explicit)
08.1. Android How to Use Intent (explicit)08.1. Android How to Use Intent (explicit)
08.1. Android How to Use Intent (explicit)
 
Android Button
Android ButtonAndroid Button
Android Button
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
 
Android App Development 03 : Widget &amp; UI
Android App Development 03 : Widget &amp; UIAndroid App Development 03 : Widget &amp; UI
Android App Development 03 : Widget &amp; UI
 
20101127
2010112720101127
20101127
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
 
android menus
android menusandroid menus
android menus
 
A comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter componentA comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter component
 

Similaire à Android Homescreen Widgets Demystified

Get an Android tutorial for beginners
Get an Android tutorial for beginnersGet an Android tutorial for beginners
Get an Android tutorial for beginnersJavaTpoint.Com
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android DevelopmentAly Abdelkareem
 
Mobile Application Development with JUCE and Native API’s
Mobile Application Development with JUCE and Native API’sMobile Application Development with JUCE and Native API’s
Mobile Application Development with JUCE and Native API’sAdam Wilson
 
01 09 - graphical user interface - basic widgets
01  09 - graphical user interface - basic widgets01  09 - graphical user interface - basic widgets
01 09 - graphical user interface - basic widgetsSiva Kumar reddy Vasipally
 
Mobile Application Development With Android
Mobile Application Development With AndroidMobile Application Development With Android
Mobile Application Development With Androidguest213e237
 
Android Tutorial For Beginners Part-1
Android Tutorial For Beginners Part-1Android Tutorial For Beginners Part-1
Android Tutorial For Beginners Part-1Amit Saxena
 
Dicoding Developer Coaching #21: Android | Cara Membuat Widget di Aplikasi An...
Dicoding Developer Coaching #21: Android | Cara Membuat Widget di Aplikasi An...Dicoding Developer Coaching #21: Android | Cara Membuat Widget di Aplikasi An...
Dicoding Developer Coaching #21: Android | Cara Membuat Widget di Aplikasi An...DicodingEvent
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to androidShrijan Tiwari
 
Lecture #3 activities and intents
Lecture #3  activities and intentsLecture #3  activities and intents
Lecture #3 activities and intentsVitali Pekelis
 
Android activity, service, and broadcast recievers
Android activity, service, and broadcast recieversAndroid activity, service, and broadcast recievers
Android activity, service, and broadcast recieversUtkarsh Mankad
 
Android training day 2
Android training day 2Android training day 2
Android training day 2Vivek Bhusal
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesAhsanul Karim
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesAhsanul Karim
 
Android Bootcamp Tanzania:understanding ui in_android
Android Bootcamp Tanzania:understanding ui in_androidAndroid Bootcamp Tanzania:understanding ui in_android
Android Bootcamp Tanzania:understanding ui in_androidDenis Minja
 
Android apps development
Android apps developmentAndroid apps development
Android apps developmentMonir Zzaman
 
Gadgets Windows
Gadgets WindowsGadgets Windows
Gadgets Windowssahibsahib
 
"Android" mobilių programėlių kūrimo įvadas #2
"Android" mobilių programėlių kūrimo įvadas #2"Android" mobilių programėlių kūrimo įvadas #2
"Android" mobilių programėlių kūrimo įvadas #2Tadas Jurelevičius
 

Similaire à Android Homescreen Widgets Demystified (20)

Get an Android tutorial for beginners
Get an Android tutorial for beginnersGet an Android tutorial for beginners
Get an Android tutorial for beginners
 
Learning Android Part 2/6
Learning Android Part 2/6Learning Android Part 2/6
Learning Android Part 2/6
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Mobile Application Development with JUCE and Native API’s
Mobile Application Development with JUCE and Native API’sMobile Application Development with JUCE and Native API’s
Mobile Application Development with JUCE and Native API’s
 
01 09 - graphical user interface - basic widgets
01  09 - graphical user interface - basic widgets01  09 - graphical user interface - basic widgets
01 09 - graphical user interface - basic widgets
 
Mobile Application Development With Android
Mobile Application Development With AndroidMobile Application Development With Android
Mobile Application Development With Android
 
Android Tutorial For Beginners Part-1
Android Tutorial For Beginners Part-1Android Tutorial For Beginners Part-1
Android Tutorial For Beginners Part-1
 
Dicoding Developer Coaching #21: Android | Cara Membuat Widget di Aplikasi An...
Dicoding Developer Coaching #21: Android | Cara Membuat Widget di Aplikasi An...Dicoding Developer Coaching #21: Android | Cara Membuat Widget di Aplikasi An...
Dicoding Developer Coaching #21: Android | Cara Membuat Widget di Aplikasi An...
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to android
 
Lecture #3 activities and intents
Lecture #3  activities and intentsLecture #3  activities and intents
Lecture #3 activities and intents
 
Android App development III
Android App development IIIAndroid App development III
Android App development III
 
Android activity, service, and broadcast recievers
Android activity, service, and broadcast recieversAndroid activity, service, and broadcast recievers
Android activity, service, and broadcast recievers
 
Android training day 2
Android training day 2Android training day 2
Android training day 2
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
 
Android session 2
Android session 2Android session 2
Android session 2
 
Android Bootcamp Tanzania:understanding ui in_android
Android Bootcamp Tanzania:understanding ui in_androidAndroid Bootcamp Tanzania:understanding ui in_android
Android Bootcamp Tanzania:understanding ui in_android
 
Android apps development
Android apps developmentAndroid apps development
Android apps development
 
Gadgets Windows
Gadgets WindowsGadgets Windows
Gadgets Windows
 
"Android" mobilių programėlių kūrimo įvadas #2
"Android" mobilių programėlių kūrimo įvadas #2"Android" mobilių programėlių kūrimo įvadas #2
"Android" mobilių programėlių kūrimo įvadas #2
 

Dernier

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Dernier (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Android Homescreen Widgets Demystified

  • 2. Hi, I'm a web developer... So many ways to create an Android application… but none of them create homescreen widgets!
  • 3. ...and I ♥ homescreen widgets Examples default Donut homescreen default Froyo homescreen
  • 4. ...and I ♥ homescreen widgets Examples Gmail Unread Count HTC SenseUI email widget
  • 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
  • 9. Designing Widgets Lighter color theme in Cupcake and Donut Darker color theme in Éclair and Froyo
  • 10. Coding Widgets App Widgets Framework Guide developer.android.com/guide/topics/appwidgets/index.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
  • 15. Overview of Widget Development 2) Declare AppWidgetProviderInfo object
  • 16. AppWidgetProviderInfo 1. Create an xml folder in the res folder. 2. In the xml folder, create a new Android XML File
  • 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
  • 29. Overview of Widget Development 4) Extend the AppWidgetProvider class
  • 30. AppWidgetProvider public class BasicWidgetProvider extends AppWidgetProvider { @Override public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate( context, appWidgetManager, appWidgetIds ); //do some stuff every updatePeroidMillis! } }
  • 31. AppWidgetProvider void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ) void onEnabled( Context context ) void onDeleted( Context context, int[] appWidgetIds ) void onDisabled( Context context ) void onReceive( Context context, Intent intent )
  • 32. AppWidgetProvider final int N = appWidgetIds.length; for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; setAlarm(context, appWidgetId, 30000); }
  • 35. Overview of Widget Development 5) Update AndroidManifest.xml