Londroid Android Home Screen Widgets

26,522 views

Published on

Published in: Technology
1 Comment
7 Likes
Statistics
Notes
  • MY question is that when i open the emulator the google and music widget is by default already on home screen...so i also want that same thing for my widget that when i open my emulator i can see my widget like google and music...
    plzzz help me and reply...
    i searched every where but i found nothing...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
26,522
On SlideShare
0
From Embeds
0
Number of Embeds
11,563
Actions
Shares
0
Downloads
254
Comments
1
Likes
7
Embeds 0
No embeds

No notes for slide

Londroid Android Home Screen Widgets

  1. 1. Android Home Screen Widgets London Android User Group 22 nd June 2009 Richard Hyndman
  2. 2. Widgets
  3. 3. BatteryWidget <ul><li>If you want to follow along I’ll be using </li></ul><ul><li>BatteryWidget as an example: </li></ul>http://batterywidget.googlecode.com Or http://geekyouup.com/batterywidget.zip
  4. 4. How? <ul><li>To create an App Widget, you need 3 things: </li></ul><ul><ul><li>AppWidgetProviderInfo xml </li></ul></ul><ul><ul><ul><li>Describes the metadata for an App Widget, such as layout, update frequency, and the AppWidgetProvider class. Defined in XML. </li></ul></ul></ul><ul><ul><li>AppWidgetProvider class implementation </li></ul></ul><ul><ul><ul><li>Defines the basic methods that allow you to programmatically interface with the App Widget, based on broadcast events. Through it, you will receive broadcasts when the App Widget is updated, enabled, disabled and deleted. </li></ul></ul></ul><ul><ul><li>View layout xml </li></ul></ul><ul><ul><ul><li>Defines the initial layout for the App Widget, defined in XML. </li></ul></ul></ul>
  5. 5. The AppWidgetProvider This tells Android O/S that your application includes a Widget and wants updates, also that the widget is defined in the file xml/widget_def.xml Declare the AppWidgetProvider class in your application's AndroidManifest.xml file.
  6. 6. AppWidgetProviderInfo <ul><li>XML file that defines the widget size and update frequency </li></ul><ul><ul><li>BatteryWidget/res/xml/widget_def.xml </li></ul></ul><ul><li>To calculate sizes: Minimum size in dip = (Number of cells * 74dip) - 2dip </li></ul><ul><li>initalLayout: The layout resource xml that is used when the widget loads </li></ul><ul><li>Don’t update more than necessary, battery life issues </li></ul><ul><li>At the update interval, your AppWidgetProvider.onUpdate() method is called </li></ul>
  7. 7. What is a DIP? <ul><li>Android documentation mentions dip’s or dp’s instead of pixels for layout, but what it is a DIP? </li></ul><ul><li>One DIP is one pixel on a 160 dpi screen </li></ul><ul><ul><li>(for example a 240x320, 1.5&quot;x2&quot; screen is approx 1dip=1px) </li></ul></ul><ul><li>On a 106 dpi screen the density is 0.75; </li></ul><ul><ul><li>1dip = 1.333px </li></ul></ul>
  8. 8. AppWidgetProvider Class <ul><li>BatteryWidget/src/com.geekyouup.android.widgets.battery.BatteryWidget </li></ul><ul><li>Problem: When your Widgets AppWidgetProvider.onUpdate(..) method is called the response is subject to the ANR (Application Not Responding) timer. If your time runs out then the user gets the “Force Close or Wait” dialog, this is not good. </li></ul><ul><li>Solution: If you need some time, start a service to process your update. It is much easier than you may think. A service just needs an onStart() method. (see BatteryWidget.java UpdateService inner class) </li></ul>
  9. 9. RemoteViews <ul><li>Problem: If Widget views aren’t dynamic and have no associated Activity, how can you update them? How can the BatteryWidget’s percentage change? </li></ul><ul><li>Solution: RemoteViews! </li></ul><ul><li>When an event triggers an update, you can attach to the Widget’s view remotely and update it using the RemoteViews methods </li></ul>
  10. 10. Widget Interactions <ul><li>Problem: If the Widget has no associated Activity, how can you make it interactive? </li></ul><ul><li>Solution: PendingIntents! </li></ul><ul><li>When using RemoteViews to update the widget layout, you can set PendingIntents on elements. For example using setOnClickPendingIntent() on the BatteryWidget starts an Activity </li></ul>Touch
  11. 11. BatteryWidget Events <ul><li>For the sake of efficiency and good coding practise the BatteryWidget does not poll the battery state. It registers as a Receiver for the Intent: Intent. ACTION_BATTERY_CHANGED </li></ul><ul><li>This intent can only be registered for in code, not as usual in the AndroidManifest.xml </li></ul>When the intent fires in BatteryInfo.java it includes extra data: intent.getIntExtra(&quot;level&quot;, 0) intent.getIntExtra(&quot;status“) which describe the current charge and charging states
  12. 12. Display / GPS / Wifi <ul><li>Display Settings </li></ul><ul><ul><li>Intent defineIntent2 = new Intent(&quot;com.android.settings.DISPLAY_SETTINGS&quot;); </li></ul></ul><ul><ul><li>defineIntent2.addCategory(&quot;android.intent.category.DEFAULT&quot;); </li></ul></ul><ul><ul><li>startActivity(defineIntent2); </li></ul></ul><ul><li>GPS Settings, how to do it properly? </li></ul><ul><ul><li>Intent defineIntent2 = new Intent(&quot;android.settings.LOCATION_SOURCE_SETTINGS&quot;); </li></ul></ul><ul><ul><li>defineIntent2.addCategory(&quot;android.intent.category.DEFAULT&quot;); </li></ul></ul><ul><ul><li>startActivity(defineIntent2); </li></ul></ul><ul><li>Wireless Settings </li></ul><ul><ul><li>WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); </li></ul></ul><ul><ul><li>boolean enabled = wifiManager.isWifiEnabled(); </li></ul></ul><ul><ul><li>wifiManager.setWifiEnabled(! enabled ); </li></ul></ul>
  13. 13. ImageView Touch Feedback <ul><li>Problem : It is important to provide feedback to the user when a button is pressed, but on a widget that can be tricky as the view isn’t dynamic. </li></ul><ul><li>Solution : Selector </li></ul><ul><li><selector xmlns:android= &quot;http://schemas.android.com/apk/res/android&quot; > </li></ul><ul><li><item android:state_pressed= &quot;true&quot; android:drawable= &quot;@drawable/edit_sel&quot; /> </li></ul><ul><li><item android:drawable= &quot;@drawable/edit&quot; /> </li></ul><ul><li></selector> </li></ul><ul><li>When an image changes state to pressed, the appropriate image will be shown, given the impression of tactile feedback </li></ul>
  14. 14. Links <ul><li>Battery Widget Source Code </li></ul><ul><ul><li>http://batterywidget.googlecode.com </li></ul></ul><ul><li>Jeff Sharkey’s Blog Post, ‘Introducing Home Screen Widgets’ includes full source code </li></ul><ul><ul><li>http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html </li></ul></ul><ul><li>Jeff Sharkey’s Weather Forecast Widget </li></ul><ul><ul><li>http://jsharkey.org/blog/2009/04/24/forecast-widget-for-android-15-with-source/ </li></ul></ul><ul><li>Android Developer Guide - AppWidgets </li></ul><ul><ul><li>http://developer.android.com/guide/topics/appwidgets/index.html </li></ul></ul>

×