{"id":567,"date":"2020-08-06T11:58:49","date_gmt":"2020-08-06T18:58:49","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/surface-duo\/?p=567"},"modified":"2020-09-24T10:48:57","modified_gmt":"2020-09-24T17:48:57","slug":"dual-screen-devices-love-widgets","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/surface-duo\/dual-screen-devices-love-widgets\/","title":{"rendered":"Dual-screen devices love widgets"},"content":{"rendered":"<p>\n  Hello Android developers!\n<\/p>\n<p>\n  In an earlier post, we talked about the <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/bring-your-app-to-surface-duo-step-2\/?WT.mc_id=blog-surfaceduoblog-conceptdev\">steps to bring your app to Microsoft Surface Duo<\/a>. One great approach to get ready for dual-screen devices like the Surface Duo is to implement Android features that work well on two screens, like app widgets. Widgets are particularly great for dual-screens because they can remain visible on one screen even as you use an app on the other. You can also be creative with the size options for your widget due to the increased screen area.\n<\/p>\n<p>\n  In today\u2019s post, we\u2019ll walk through a widget example for the Surface Duo, which can run on any Android device.\n<\/p>\n<h2>Surface Duo blog reader widget<\/h2>\n<p>\n  The <a href=\"https:\/\/github.com\/microsoft\/surface-duo-app-samples\/tree\/main\/Widget\">blog reader widget sample code<\/a> provides a complete widget implementation, including an RSS feed reader that can display a blog summary on your phone\u2019s home screen. When you touch a blog post in the widget, the full text is opened in Microsoft Edge, as shown in this screenshot:\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"1168\" height=\"781\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/08\/word-image.png\" class=\"wp-image-568\" alt=\"Surface Duo emulator showing a widget with blog list and web browser with blog article\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/08\/word-image.png 1168w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/08\/word-image-300x201.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/08\/word-image-1024x685.png 1024w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/08\/word-image-768x514.png 768w\" sizes=\"(max-width: 1168px) 100vw, 1168px\" \/>\n<\/p>\n<p><em>Figure 1: Blog reader widget and post showing in Microsoft Edge<\/em>\n<\/p>\n<p>\n  App widgets are a special kind of view, with some restrictions on how the user can interact with them (due to their presence on the home screen). Only touches and vertical scrolling is allowed. There is also a specific mechanism you must use to update the data shown in the widget. The high level steps are shown below, and you can also visit the <a href=\"https:\/\/developer.android.com\/guide\/topics\/appwidgets\/overview\">documentation for App Widgets<\/a> for more information.\n<\/p>\n<h3>AndroidManifest.xml<\/h3>\n<p>\n  The following entry in <strong>AndroidManifest.xml<\/strong> declares the widget:\n<\/p>\n<pre>&lt;receiver android:name=\".WidgetApp\">\r\n    &lt;intent-filter>\r\n        &lt;action android:name=\"android.appwidget.action.APPWIDGET_UPDATE\" \/>\r\n    &lt;\/intent-filter>\r\n\r\n    &lt;meta-data\r\n        android:name=\"android.appwidget.provider\"\r\n        android:resource=\"@xml\/app_widget_info\" \/>\r\n&lt;\/receiver>\r\n<\/pre>\n<p>\n  There are three important parts in this declaration:\n<\/p>\n<ul>\n<li>\n    The first attribute references the <a href=\"https:\/\/github.com\/microsoft\/surface-duo-app-samples\/blob\/main\/Widget\/app\/src\/main\/java\/com\/microsoft\/device\/display\/samples\/widget\/WidgetApp.kt\">WidgetApp<\/a> class which implements <strong>AppWidgetProvider<\/strong>.\n  <\/li>\n<li>\n    The second element identifies the <strong>APPWIDGET_UPDATE<\/strong> intent filter \u2013 this is required.\n  <\/li>\n<li>\n    The third element links to the metadata that controls how the widget is rendered.\n  <\/li>\n<\/ul>\n<h3>WidgetApp class<\/h3>\n<p>\n  The WidgetApp class must implement <strong>AppWidgetProvider<\/strong> and provide an <strong>onUpdate<\/strong> method which gets called every update period. There can be multiple instances of the widget added to the home screen so the update method must deal with a collection of widgets, updating each one appropriately.\n<\/p>\n<p><strong>onUpdate<\/strong> is the only required overload, however you can also overrode onReceive to handle other callbacks such as ACTION_APPWIDGET_UPDATE as well as delated, enabled, disabled, and options changed intents.\n<\/p>\n<h3>Widget metadata<\/h3>\n<p>\n  Notice it refers to an <strong>app_widget_info.xml<\/strong> file, which contains widget configuration information needed for an <strong>AppWidgetProviderInfo<\/strong> object. The XML file specifies information like the layout for the widget\u2019s user interface, its initial dimensions, whether it\u2019s resizable, and the data refresh frequency:\n<\/p>\n<pre>&lt;appwidget-provider xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:initialKeyguardLayout=\"@layout\/app_widget\"\r\n    android:initialLayout=\"@layout\/app_widget\"\r\n    android:minWidth=\"250dp\"\r\n    android:minHeight=\"250dp\"\r\n    android:previewImage=\"@drawable\/widget_preview\"\r\n    android:resizeMode=\"horizontal|vertical\"\r\n    android:updatePeriodMillis=\"86400000\"\r\n    android:widgetCategory=\"home_screen\"\/>\r\n<\/pre>\n<p>\n  Note that the update frequency can have an effect on battery life \u2013 see the documentation for an alternative using <a href=\"https:\/\/developer.android.com\/guide\/topics\/appwidgets\/overview\">AlarmManager<\/a>.\n<\/p>\n<h3>Widget functionality<\/h3>\n<p>\n  The RSS blog reader sample uses a custom <a href=\"https:\/\/github.com\/microsoft\/surface-duo-app-samples\/blob\/main\/Widget\/app\/src\/main\/java\/com\/microsoft\/device\/display\/samples\/widget\/feed\/RssFeed.kt\"><strong>RssFeed<\/strong><\/a> class to download and parse the blog XML and render the list in the widget. Refer to our <a href=\"https:\/\/github.com\/microsoft\/surface-duo-app-samples\/blob\/main\/Widget\/\">sample repo<\/a> for more details on the download and display of the blog data, and the <a href=\"https:\/\/developer.android.com\/guide\/topics\/appwidgets\/overview\">Android widget docs<\/a> for more details on widget customization.\n<\/p>\n<h2>Resources and feedback<\/h2>\n<p>\n  We would love to hear from you about your experiences building apps and widgets for the Surface Duo. Please visit the <a href=\"https:\/\/docs.microsoft.com\/dual-screen\/android\/?WT.mc_id=docs-surfaceduoblog-conceptdev\">docs<\/a> and <a href=\"https:\/\/github.com\/microsoft\/surface-duo-app-samples\">samples<\/a>, and let us know what you think.\n<\/p>\n<p>\n  Reach out using our\u00a0<a href=\"http:\/\/aka.ms\/SurfaceDuoSDK-Feedback\" target=\"_blank\" rel=\"noopener noreferrer\">feedback forum<\/a>\u00a0or message me on\u00a0<a href=\"https:\/\/twitter.com\/conceptdev\" target=\"_blank\" rel=\"noopener noreferrer\">Twitter<\/a>\u00a0or\u00a0<a href=\"https:\/\/github.com\/conceptdev\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a>.\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Android developers! In an earlier post, we talked about the steps to bring your app to Microsoft Surface Duo. One great approach to get ready for dual-screen devices like the Surface Duo is to implement Android features that work well on two screens, like app widgets. Widgets are particularly great for dual-screens because they [&hellip;]<\/p>\n","protected":false},"author":570,"featured_media":574,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,46],"class_list":["post-567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-surface-duo-sdk","tag-dual-screen-development","tag-surface-duo"],"acf":[],"blog_post_summary":"<p>Hello Android developers! In an earlier post, we talked about the steps to bring your app to Microsoft Surface Duo. One great approach to get ready for dual-screen devices like the Surface Duo is to implement Android features that work well on two screens, like app widgets. Widgets are particularly great for dual-screens because they [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/users\/570"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/comments?post=567"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/567\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media\/574"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media?parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/categories?post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/tags?post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}