{"id":20194,"date":"2015-08-03T15:34:22","date_gmt":"2015-08-03T19:34:22","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=20194"},"modified":"2015-08-03T15:34:22","modified_gmt":"2015-08-03T19:34:22","slug":"bringing-xamarin-forms-apps-to-tablets","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/bringing-xamarin-forms-apps-to-tablets\/","title":{"rendered":"Bringing Xamarin.Forms Apps to Tablets"},"content":{"rendered":"<p>\t\t\t\tWhen I was developing <a href=\"https:\/\/blog.xamarin.com\/connect-to-customers-with-my-shoppe-template-app\/\" title=\"My Shoppe App\">My Shoppe<\/a>, a pre-built app template for shops, I only focused on the look and feel for the phone form factor, since I knew that I could add optimizations to the app to spruce it up for tablet owners later. Xamarin.Forms makes it simple to detect what type of device my users are running and provides a few nice helper methods to extend the app for a better tablet experience. Here are a few tips to get you started with optimizing your Xamarin.Forms apps for these larger form factors.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-large wp-image-20202\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/MyShoppeFamily-1024x736.png\" alt=\"MyShoppeFamily\" width=\"1024\" height=\"736\" \/><\/p>\n<h2>Adding Tablet Support<\/h2>\n<p>The first thing to do is ensure that tablet support is enabled for the app, which each platform handles a little bit differently. On iOS, it&#8217;s simple to adjust what type of devices an app can be run on: simply click on the properties for the iOS project and under <b>iOS Application<\/b> is a setting called <b>Devices<\/b>. This controls the final output of the app for either iPhone\/iPod, iPad, or Universal. For My Shoppe, I switched from iPhone\/iPod over to <b>Universal<\/b> to ensure the app has a proper phone and tablet form factor when running on either of the devices instead of a <b>2x<\/b> zoom button when running the app on the iPad.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-20206\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/devices.png\" alt=\"devices\" width=\"961\" height=\"346\" \/><\/p>\n<p>Android handles tablet support a little differently since Android works with pixels in correlation to densities, and any app can be scaled correctly up to a larger screen. There are a few different <a href=\"http:\/\/developer.android.com\/google\/play\/filters.html\" title=\"Filters for Google Play\">options<\/a> that can be added to the Android manifest to filter the app out of Google Play. By default, there are no adjustments that need to get My Shoppe running on tablets correctly.<\/p>\n<h2>Optimizing with Device.Idiom<\/h2>\n<p>Simply enabling tablet support isn&#8217;t enough, and I wanted to finely-tune specific areas of the app to make\u00a0using My Shoppe on tablets an excellent user experience.\u00a0Xamarin.Forms provides many different ways to <a href=\"http:\/\/developer.xamarin.com\/guides\/cross-platform\/xamarin-forms\/working-with\/platform-specifics\/\" title=\"Platform specific tweaks in Xamarin.Forms\">add platform-specific tweaks<\/a> to layouts through different properties the <code>Device<\/code> class exposes. You may already be familiar with <b>Device.OS<\/b> or <b>Device.OnPlatform<\/b> that can be used to add OS level tweaks, but we can use <b>Device.Idiom<\/b> to detect if the current device is a phone or tablet. When building a layout for tablet, I often like to switch StackLayouts from vertical to horizontal and display different images when my users are on a tablet. This can be accomplished easily:<\/p>\n<pre class=\"lang:csharp decode:true\">\nif (Device.Idiom == TargetIdiom.Phone)\n{\n    MyStack.Orientation = StackOrientation.Vertical;\n    HeroImage.Source = ImageSource.FromFile(\"hero.jpg\");\n}\nelse\n{\n    MyStack.Orientation = StackOrientation.Horizontal;\n    HeroImage.Source = ImageSource.FromFile(\"herotablet.jpg\");\n}\n<\/pre>\n<p>In My Shoppe, I performed these optimizations to take advantage of the additional screen size, and in all of my grid layouts I added additional padding and spacing directly in my XAML:<\/p>\n<pre class=\"lang:xml decode:true\">\n\n  \n    \n \n  \n    \n  \n  \n    \n  \n  <!-- Grid Content -->\n\n<\/pre>\n<p><img decoding=\"async\" class=\"aligncenter size-large wp-image-20200\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/SpacingOptimizations-1024x530.png\" alt=\"SpacingOptimizations\" width=\"1024\" height=\"530\" \/><\/p>\n<h2>Leverage MasterDetailPage<\/h2>\n<p>Simple platform tweaks are great and can go a long way, but there&#8217;s certainly more that can be done to optimize the UX on tablets.\u00a0I decided that users would see a different page based on the type of device they were using when they tap to see all shop locations. On a phone, the flow wouldn&#8217;t change, as a full list of locations would appear and they could drill down to details. On tablets,\u00a0I decided to navigate to a MasterDetailPage that has a full flyout menu where the locations could be listed and the shop details would be displayed side by side. I accomplished this by re-directing the app to different screens based on the Idiom that was detected. I was still able to leverage all of the existing code in the Store list page by propagating the click event up to the MasterDetailPage to swap out the Detailed information.<\/p>\n<h3>Propagate the event<\/h3>\n<p>On my original StoresPage I added a new <b>Action<\/b> that could be used to propagate the event to the MasterDetailPage:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic partial class StoresPage : ContentPage\n{\n  public Action ItemSelected { get; set; }\n  public StoresPage ()\n  {\n    InitializeComponent ();\n\n    \/\/Fill in Binding Context...\n\n    \/\/Handle item selected to navigate to new page or propagate event\n    StoreList.ItemSelected += async (sender, e) =&gt;\n    {\n      if(StoreList.SelectedItem == null)\n        return;\n      var store = e.SelectedItem as Store;\n      if (ItemSelected == null)\n      {\n        await Navigation.PushAsync(new StorePage(store));\n        StoreList.SelectedItem = null;\n      }\n      else\n      {\n        ItemSelected.Invoke(store);\n      }\n    };\n  }\n}\n<\/pre>\n<h3>New Tablet Page<\/h3>\n<p>I created a new MasterDetailPage that set the StoresPage to the Master and a temporary Detail page asking the user to select a shop. Next,\u00a0I simply subscribed to the ItemSelected action and swapped out the Detail page:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic class StoresTabletPage : MasterDetailPage\n{\n  public StoresTabletPage()\n  {\n    Title = \"Stores\";\n    Master = new StoresPage();\n    Detail = new ContentPage\n    {\n      Content = new StackLayout\n      {\n        VerticalOptions = LayoutOptions.Center,\n        HorizontalOptions = LayoutOptions.Center,\n        Children =\n        {\n          new Label { Text = \"Select a Shop\", FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)) }\n        }\n      }\n    };\n\n    \/\/Subscribe to ItemSelected changes and update Detail Page\n    ((StoresPage)Master).ItemSelected = (store) =&gt;\n    {\n      Detail = new StorePage(store);\n      if(Device.OS != TargetPlatform.Windows)\n        IsPresented = false;\n     };\n\n   IsPresented = true;\n }\n}\n<\/pre>\n<h3>Updating the Workflow<\/h3>\n<p>With the new tablet optimized section of the app ready, all I needed to do was update my Home page to navigate to a different page based on the Idiom:<\/p>\n<pre class=\"lang:csharp decode:true\">\nButtonFindStore.Clicked += async (sender, e) =&gt;\n{\n  if (Device.Idiom == TargetIdiom.Tablet || Device.Idiom == TargetIdiom.Desktop)\n    await Navigation.PushAsync(new StoresTabletPage());\n  else\n    await Navigation.PushAsync(new StoresPage());\n};\n<\/pre>\n<p><img decoding=\"async\" class=\"aligncenter size-large wp-image-20201\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/MasterDetailOptimization-1024x356.png\" alt=\"MasterDetailOptimization\" width=\"1024\" height=\"356\" \/><\/p>\n<h3>Adding Windows Support<\/h3>\n<p>My Shoppe also launched on Windows Phone, but Windows Phone is just for phones, so there are no changes that I had to make to that project. Since Xamarin.Forms now supports Windows apps, this is another tablet and desktop form factor that My Shoppe could be extended to. The setup process was just to follow the <a href=\"http:\/\/developer.xamarin.com\/guides\/cross-platform\/xamarin-forms\/windows\/\" title=\"Setup Guide for Xamarin.Forms on Windows\">step-by-step documentation<\/a> to add the Windows project. After I had the project set up, I only had to install a handful of <a href=\"https:\/\/github.com\/xamarin\/plugins\" title=\"Plugins for Xamarin\">Plugins for Xamarin<\/a>, which I use to tap into native capabilities from shared code and add a few Xamarin.Forms custom controls, such as Image Circles. With all the NuGets and project setup in place, My Shoppe was now on Windows desktops and tablets!<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-large wp-image-20204\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Windows-Store-My-Shoppe-1024x639.png\" alt=\"Windows Store My Shoppe\" width=\"1024\" height=\"639\" \/><\/p>\n<h3>Bring Your Xamarin.Forms Apps to Tablets<\/h3>\n<p>Optimizations such as these can greatly enhance your users&#8217; experiences in your apps and are very simple implement with the built-in tools included in Xamarin.Forms. Be sure to read through the <a href=\"http:\/\/developer.xamarin.com\/guides\/cross-platform\/xamarin-forms\/working-with\/platform-specifics\/\">Working with Platform Tweaks<\/a> documentation to find additional ways of optimizing your Xamarin.Forms apps, and don&#8217;t forget to grab the My Shoppe source code with these tweaks, now available on\u00a0<a href=\"https:\/\/github.com\/jamesmontemagno\/myshoppe\" title=\"My Shoppe on GitHub\">GitHub<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I was developing My Shoppe, a pre-built app template for shops, I only focused on the look and feel for the phone form factor, since I knew that I could add optimizations to the app to spruce it up for tablet owners later. Xamarin.Forms makes it simple to detect what type of device my [&hellip;]<\/p>\n","protected":false},"author":544,"featured_media":20202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4,16],"class_list":["post-20194","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>When I was developing My Shoppe, a pre-built app template for shops, I only focused on the look and feel for the phone form factor, since I knew that I could add optimizations to the app to spruce it up for tablet owners later. Xamarin.Forms makes it simple to detect what type of device my [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/20194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=20194"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/20194\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=20194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=20194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=20194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}