{"id":1708,"date":"2021-07-15T12:38:17","date_gmt":"2021-07-15T19:38:17","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/surface-duo\/?p=1708"},"modified":"2021-07-15T12:38:17","modified_gmt":"2021-07-15T19:38:17","slug":"jetpack-compose-twopanelayout-preview","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/surface-duo\/jetpack-compose-twopanelayout-preview\/","title":{"rendered":"New TwoPaneLayout Compose library preview"},"content":{"rendered":"<p>\n  Hello Android developers!\n<\/p>\n<p>\n  Google just announced the <a href=\"https:\/\/developer.android.com\/jetpack\/androidx\/releases\/compose\">first release candidate for Compose<\/a> on 07\/01. We are getting one big step closer to stable this month! In previous blogs, <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/jetpack-compose-foldable-samples\/\">Jetpack Compose on foldable and dual-screen development<\/a> and <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/jetpack-compose-dual-screen-sample\/\">Jetpack Compose on Microsoft Surface Duo,<\/a> <a id=\"post-1708-_Int_b8cB49Ht\"><\/a>we mentioned that there are two main steps you can implement to develop or enhance your application to leverage these new form factors using this new UI toolkit:\n<\/p>\n<ol>\n<li>\n  Integrate Jetpack Window Manager to detect the information of the device and the bounds of the hinge\/folding line.\n<\/li>\n<li>\n  Update the UI based on the device orientation and the information from Jetpack Window Manager.\n<\/li>\n<\/ol>\n<h2>TwoPaneLayout preview<\/h2>\n<p>\n  Today we are introducing a new library, <strong>TwoPaneLayout<\/strong>, which can help you simplify this implementation. You can easily integrate the library when using Compose to build a two-pane layout. TwoPaneLayout provides a layout container to hold two different layouts horizontally or vertically. It will switch automatically based on the device orientation. The two panes will be placed side-by-side according to the hinge (width may not be zero) on a dual-screen device or the folding line on a foldable device, if the layout parameter \u201cweight\u201d is not configured, otherwise the two panes will be divided based on the parameter configuration. \n<\/p>\n<p>\n  <img decoding=\"async\" width=\"936\" height=\"488\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-1.png\" class=\"wp-image-1709\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-1.png 936w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-1-300x156.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-1-768x400.png 768w\" sizes=\"(max-width: 936px) 100vw, 936px\" \/><br\/><em>Figure 1: TwoPaneLayout on Foldable\/Dual-screen devices<\/em>\n<\/p>\n<p>\n  When the foldable and dual-screen devices become unfolded or unspanned, or the app is running on a regular single-screen device, the two-pane layout would be converted to a single-pane layout without any extra effort. And we also scale the library to support large-screen devices, such as a tablet, which can provide a split-view style to enable multi-tasking on one large display area.\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"856\" height=\"324\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-2.png\" class=\"wp-image-1710\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-2.png 856w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-2-300x114.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-2-768x291.png 768w\" sizes=\"(max-width: 856px) 100vw, 856px\" \/><br\/><em>Figure 2: TwoPaneLayout on single-screen device<\/em>\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"936\" height=\"434\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-3.png\" class=\"wp-image-1711\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-3.png 936w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-3-300x139.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-3-768x356.png 768w\" sizes=\"(max-width: 936px) 100vw, 936px\" \/><br\/><em>Figure 3: TwoPaneLayout on large-screen device (such as a tablet)<\/em>\n<\/p>\n<p>\n  Two-pane layout can be used in many scenarios on foldable, dual-screen, and large-screen devices, including <a href=\"https:\/\/docs.microsoft.com\/en-us\/dual-screen\/design\/list-detail\">list detail<\/a>, <a href=\"https:\/\/docs.microsoft.com\/en-us\/dual-screen\/design\/dual-view\">dual view<\/a>, <a href=\"https:\/\/docs.microsoft.com\/en-us\/dual-screen\/design\/companion-pane\">companion pane<\/a>, and the three design pattern we mentioned before. With them, the applications can display list and detail content, or content with control, or list and map at the same time, regardless of the device orientation.\n<\/p>\n<h2>Try it out!<strong> <\/strong><\/h2>\n<p>\n  The TwoPaneLayout is very easy to use. You can integrate it into your Android Studio project in four steps:\n<\/p>\n<ol>\n<li>\n<p>\nIn the top-level <strong>build.gradle<\/strong> file, make sure you have <strong>mavenCentral()<\/strong> repository\n<\/p>\n<pre>repositories {\r\n     google()\r\n     mavenCentral()\r\n }<\/pre>\n<\/li>\n<li>\n<p>In the module-level <strong>build.gradle<\/strong> file, add the following dependency<\/p>\n<pre>implementation \"com.microsoft.device.dualscreen:twopanelayout:1.0.0-alpha02\"<\/pre>\n<\/li>\n<li>\n<p>Also ensure the <strong>compileSdkVersion<\/strong> and <strong>targetSdkVersion<\/strong> are set to API <strong>30<\/strong> or newer in the module-level <strong>build.gradle<\/strong> file.<\/p>\n<pre>android {\r\n    compileSdkVersion 30\r\n \r\n    defaultConfig {\r\n        targetSdkVersion 30\r\n    }\r\n    ...\r\n}<\/pre>\n<\/li>\n<li>\n<p>Start building your layout using TwoPaneLayout with just one line of code. Then you can have two different customized layouts showing side-by-side or above-and-below, or a single prioritized layout.\n<\/p>\n<p>\n  Here is a small <a href=\"https:\/\/github.com\/microsoft\/surface-duo-compose-samples\/tree\/main\/TwoPaneLayout\/sample\">code sample<\/a> to demonstrate the use of <strong>TwoPaneLayout:<\/strong> \n<\/p>\n<pre>TwoPaneLayout {\r\n    Text(\r\n        text = stringResource(R.string.first_pane_text),\r\n        modifier = Modifier.fillMaxSize().background(color = Color.Cyan)\r\n    )\r\n    Text(\r\n        text = stringResource(R.string.second_pane_text),\r\n        modifier = Modifier.fillMaxSize().background(color = Color.Magenta)\r\n    )\r\n}  <\/pre>\n<\/li>\n<\/ol>\n<p>\n  There are two child elements which are two Composable text labels created and placed inside TwoPaneLayout. The library will position the elements based on the order, which means the first element will be placed in the first pane and the second element will be placed in the second pane. Since no \u201cweight\u201d is configured, the first child element will be prioritized in the single-screen mode.\n<\/p>\n<p>\n  If running the sample on a single-screen device, foldable, or dual-screen device, such as Surface Duo, or a tablet, you can see the two panes will be displayed accordingly based on the device type, state, and orientation without any code changes.\n<\/p>\n<p>\n<a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/Screenshot_1626126539_framed_600.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/Screenshot_1626126539_framed_600.png\" alt=\"TwoPaneLayout sample on single screen phone\" width=\"324\" height=\"600\" class=\"alignnone size-full wp-image-1725\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/Screenshot_1626126539_framed_600.png 324w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/Screenshot_1626126539_framed_600-162x300.png 162w\" sizes=\"(max-width: 324px) 100vw, 324px\" \/><\/a><br\/><em>Figure 4: Sample without \u201cweight\u201d configured on a single-screen device<\/em>\n<\/p>\n<p>   \n  <img decoding=\"async\" width=\"1082\" height=\"838\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-5.png\" class=\"wp-image-1713\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-5.png 1082w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-5-300x232.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-5-1024x793.png 1024w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/word-image-5-768x595.png 768w\" sizes=\"(max-width: 1082px) 100vw, 1082px\" \/><br\/><em>Figure 5: Sample without \u201cweight\u201d configured on a dual-screen device (Surface Duo)<\/em>\n<\/p>\n<p>\n  <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/twopanelayout-galaxy-fold-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/twopanelayout-galaxy-fold-2.png\" alt=\"Foldable devices running TwoPaneLayout sample\" width=\"631\" height=\"510\" class=\"alignnone size-full wp-image-1732\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/twopanelayout-galaxy-fold-2.png 631w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/twopanelayout-galaxy-fold-2-300x242.png 300w\" sizes=\"(max-width: 631px) 100vw, 631px\" \/><\/a><\/br><em>Figure 6: Sample without \u201cweight\u201d configured on a foldable device rotated 90 degrees<\/em>\n<\/p>\n<p>\n  If we updated the above code by adding the layout parameter <code>weight<\/code>, the two panes will be adjusted proportionally based on that. And in the single-screen mode, the element with higher weight will be prioritized to show.\n<\/p>\n<pre>TwoPaneLayout {\r\n    Text(\r\n        text = stringResource(R.string.first_pane_text),\r\n        modifier = Modifier.fillMaxSize().background(color = Color.Cyan).weight(.3f)\r\n    )\r\n    Text(\r\n        text = stringResource(R.string.second_pane_text),\r\n        modifier = Modifier.fillMaxSize().background(color = Color.Magenta).weight(.7f)\r\n    )\r\n}  <\/pre>\n<p>\n  <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/single-screen-cyan-600.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/single-screen-cyan-600.png\" alt=\"Surface Duo showing TwoPaneLayout second pane\" width=\"324\" height=\"600\" class=\"alignnone size-full wp-image-1727\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/single-screen-cyan-600.png 324w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/single-screen-cyan-600-162x300.png 162w\" sizes=\"(max-width: 324px) 100vw, 324px\" \/><\/a><br\/><em>Figure 7: Sample with \u201cweight\u201d configured on a single-screen device<\/em>\n<\/p>\n<p>\n <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/tablet-transparent.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/tablet-transparent.png\" alt=\"Android tablet showing TwoPaneLayout sample\" width=\"947\" height=\"707\" class=\"alignnone size-full wp-image-1730\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/tablet-transparent.png 947w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/tablet-transparent-300x224.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/07\/tablet-transparent-768x573.png 768w\" sizes=\"(max-width: 947px) 100vw, 947px\" \/><\/a><br \/><em>Figure 8: Sample with \u201cweight\u201d configured on a large-screen device (tablet)<\/em>\n<\/p>\n<p><strong>Resources and feedback<\/strong>\n<\/p>\n<p>\n  The source code here is <a href=\"https:\/\/github.com\/microsoft\/surface-duo-compose-samples\/tree\/main\/TwoPaneLayout\">available on GitHub<\/a>; we\u2019d love to hear your feedback or contributions to the project.\n<\/p>\n<p>\n  Check out the <a href=\"https:\/\/docs.microsoft.com\/dual-screen\/android\/sample-code\/launch-to-second-screen\/?WT.mc_id=docs-surfaceduoblog-CesarValiente\">Surface Duo developer documentation<\/a> and <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/dual-screen-react-web\/\">past blog posts<\/a> for links and details on all our samples.\n<\/p>\n<p>\n  If you have any questions, or would like to tell us about your apps, use the <a href=\"http:\/\/aka.ms\/SurfaceDuoSDK-Feedback\">feedback forum<\/a> or message us on Twitter <a href=\"https:\/\/twitter.com\/surfaceduodev\">@surfaceduodev<\/a>.\n<\/p>\n<p>\n  You can also <a href=\"https:\/\/www.twitch.tv\/surfaceduodev\">chat with us on Twitch<\/a> on Friday, July 16th at 11am PST, where you can ask questions about Jetpack Compose or any other aspect of developing dual-screen apps for the Surface Duo!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Android developers! Google just announced the first release candidate for Compose on 07\/01. We are getting one big step closer to stable this month! In previous blogs, Jetpack Compose on foldable and dual-screen development and Jetpack Compose on Microsoft Surface Duo, we mentioned that there are two main steps you can implement to develop [&hellip;]<\/p>\n","protected":false},"author":30456,"featured_media":1713,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[692,473,46],"class_list":["post-1708","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-surface-duo-sdk","tag-jetpack-compose","tag-kotlin","tag-surface-duo"],"acf":[],"blog_post_summary":"<p>Hello Android developers! Google just announced the first release candidate for Compose on 07\/01. We are getting one big step closer to stable this month! In previous blogs, Jetpack Compose on foldable and dual-screen development and Jetpack Compose on Microsoft Surface Duo, we mentioned that there are two main steps you can implement to develop [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/1708","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\/30456"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/comments?post=1708"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/1708\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media\/1713"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media?parent=1708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/categories?post=1708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/tags?post=1708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}