Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Londroid Android Home Screen Widgets


Published on

Published in: Technology
  • MY question is that when i open the emulator the google and music widget is by default already on home 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...
    Are you sure you want to  Yes  No
    Your message goes here

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> Or
  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/ </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 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 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;;); </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;; > </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> </li></ul></ul><ul><li>Jeff Sharkey’s Blog Post, ‘Introducing Home Screen Widgets’ includes full source code </li></ul><ul><ul><li> </li></ul></ul><ul><li>Jeff Sharkey’s Weather Forecast Widget </li></ul><ul><ul><li> </li></ul></ul><ul><li>Android Developer Guide - AppWidgets </li></ul><ul><ul><li> </li></ul></ul>