{"id":19806,"date":"2015-07-13T16:35:00","date_gmt":"2015-07-13T23:35:00","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=19806"},"modified":"2015-07-13T16:35:00","modified_gmt":"2015-07-13T23:35:00","slug":"add-beautiful-material-design-with-the-android-support-design-library","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/add-beautiful-material-design-with-the-android-support-design-library\/","title":{"rendered":"Beautiful Material Design with the Android Support Design Library"},"content":{"rendered":"<p>\t\t\t\tIt was clear when Google introduced Material Design that it would shift the Android landscape even more than its Holo predecessor, and thus far the journey for developers to implement those new design principles in their apps has been a bit of a long and bumpy ride.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/materialdesign_introduction-1024x551-1024x551.png\" alt=\"Material Design for Android Applications Introduction\" width=\"300\" class=\"alignright size-large wp-image-19815\" \/><\/p>\n<p>In the beginning, Material Design was only available on devices running Android 5.0+ Lollipop, but over time the <a href=\"https:\/\/blog.xamarin.com\/android-tips-hello-appcompatactivity-goodbye-actionbaractivity\/\" title=\"Implementing AppCompat with Support v7\">Support Library v7 AppCompat was updated to enable Material theming<\/a> on older versions of Android.<\/p>\n<p>Theming, though, is only a part of the equation. Material Design also comprised a lot of new UI components and interactions that were not present in AppCompat. This is where the new Android Support Design Library comes in, offering a wide range of implementations of several portions of the Material Design specification in a fully backwards compatible fashion when combined with Support Library v7 AppCompat.<\/p>\n<h2>Get The Support Design Library<\/h2>\n<p>If you haven&#8217;t gotten up to speed yet on switching your apps to use <code>AppCompatActivity<\/code> and AppCompat theming, be sure to first <a href=\"https:\/\/blog.xamarin.com\/android-tips-hello-appcompatactivity-goodbye-actionbaractivity\/\" title=\"Implementing AppCompat with Support v7\">read through our entire blog post<\/a> on how to get set up. Once your app has been AppCompat enabled, head over to the component store from inside of Visual Studio or Xamarin Studio and Search for &#8220;Android Support Design&#8221;.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/ComponentSupportDesign-1024x225.png\" alt=\"Component Support Design for Android Applications\" width=\"1024\" height=\"225\" class=\"aligncenter size-large wp-image-19807\" \/><\/p>\n<p>Add the component to your project and the Support Design Library NuGet, and samples will automatically be installed in your project. Be sure to browse the sample for an overview of everything that Support Design Library has to offer.<\/p>\n<p>Now that you&#8217;re set up, let&#8217;s take a look at a few of my favorite new widgets that are built into this awesome library.<\/p>\n<h2>Floating Labels with TextInputLayout<\/h2>\n<p>The <code>EditText<\/code> widget offers a wide range of customizations, including my favorite, the <code>Hint<\/code> property, but there is one small issue with it. While the Hint property displays an indication of what your users should enter when in its unfocused state, it vanishes when the user taps the control or if anything is entered in the EditText field. This usually means you have to add a separate label to your fields to add context.<\/p>\n<p>Not anymore! The TextInputLayout can now wrap any EditText and will automatically animate the Hint to be displayed right above the EditText widget.<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;android.support.design.widget.TextInputLayout\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;wrap_content&quot;&gt;\n    &lt;EditText\n        android:id=&quot;@+id\/username&quot;\n        android:layout_width=&quot;fill_parent&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:hint=&quot;Username&quot; \/&gt;\n&lt;\/android.support.design.widget.TextInputLayout&gt;\n<\/pre>\n<p>Here it is in action:\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/FloatingLabel.gif\" alt=\"Android Application - Floating Label\" width=\"480\" height=\"360\" class=\"aligncenter size-full wp-image-19809\" \/><\/p>\n<h2>Snackbar: Powerful &amp; Enhanced Toast<\/h2>\n<p><a href=\"http:\/\/developer.xamarin.com\/api\/type\/Android.Widget.Toast\/\" title=\"Toast on Android\">Toasts<\/a> on Android are normally used to display short lived information that has no interaction.<\/p>\n<p>The new <code>Snackbar<\/code> component on the other hand, provides a similar lightweight feedback mechanism but lets you specify extra actions available to users. If you&#8217;ve used toasts before, the core API will look familiar:<\/p>\n<pre class=\"lang:csharp decode:true\">\nSnackbar\n  .Make (parentLayout, &quot;Message sent&quot;, Snackbar.LengthLong)\n  .SetAction (&quot;Undo&quot;, (view) =&gt; { \/*Undo message sending here.*\/ })\n  .Show (); \/\/ Don\u2019t forget to show!\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/components_toasts_usage5-300x107.png\" alt=\"Android Application Components - Toasts\" width=\"300\" height=\"107\" class=\"aligncenter size-medium wp-image-19808\" \/><\/p>\n<p>You should note the use of a <code>View<\/code> as the first parameter to <code>Make ()<\/code> &mdash; Snackbar will attempt to find an appropriate parent of the Snackbar&#8217;s view to ensure that it&#8217;s anchored to the bottom.<\/p>\n<p>There are several instances where the Snackbar will come in handy. Take the example above of a login page: you may want to use the new Snackbar to display information when the user attempts to log in. Additionally, if your user enters an incorrect password, you could supply an optional action to clear the password field:<\/p>\n<pre class=\"lang:csharp decode:true\">\nloginButton.Click += (sender, e) =&gt; {\n  if (string.IsNullOrWhiteSpace (username.Text)) {\n    Snackbar.Make (drawerLayout, &quot;Fill in username.&quot;, Snackbar.LengthLong)\n\t    .SetAction (&quot;OK&quot;, (v) =&gt; { })\n\t    .Show ();\n  } else if (password.Text != &quot;monkey&quot;) {\n    Snackbar.Make (parentLayout, &quot;Invalid Password&quot;, Snackbar.LengthLong)\n            .SetAction (&quot;Clear&quot;, (v) =&gt; { password.Text = string.Empty; })\n            .Show (); \n  } else {\n    Snackbar.Make (parentLayout, &quot;You are now Logged in!&quot;, Snackbar.LengthLong)\n            .Show ();\n  }\n};\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/snackbar.gif\" alt=\"Snackbar in Android Application\" width=\"216\" height=\"360\" class=\"aligncenter size-full wp-image-19810\" \/><\/p>\n<h2>Effective Navigation in Android<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/NavigationDrawer-595x1024.png\" alt=\"Navigation Drawer using Material Design in Android Application\" width=\"275\" class=\"alignright size-large wp-image-19812\" \/>There are several ways to handle navigation in your app, including dashboards, tabs, and even drop down spinners. My favorite, though, is the Navigation Drawer.<\/p>\n<p>By providing a simple, yet highly customizable slide out menu, the Navigation Drawer is an ideal pattern for a lot of apps. Implementing the Navigation Drawer was always a bit of a pain, but this has been greatly simplified by combing the new  <code>NavigationView<\/code> class with the standard <code>DrawerLayout<\/code>.<\/p>\n<p>The <code>NavigationView<\/code> makes it easier to display menu items by leveraging the standard menu resource system that you&#8217;re already familiar with. It also provides a simplified way of adding a header layout to the top of your NavigationView. You can easily have a beautiful and functional Navigation Drawer up and running with just a few lines of code.<\/p>\n<h3>Setup<\/h3>\n<p>The root of your app will be a standard Android XML that will house three important items:<\/p>\n<ul>\n<li>DrawerLayout: Top-level container for an interactive drawer<\/li>\n<li>Your Content: What you want to display on your main activity (usually a fragment that is swapped out)<\/li>\n<li>NavigationView: Easy way of implementing the navigation drawer and inflating menu items<\/li>\n<\/ul>\n<p>Here is the standard code for your Android XML:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;android.support.v4.widget.DrawerLayout\n        xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n        xmlns:app=&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot;\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;match_parent&quot;\n        android:fitsSystemWindows=&quot;true&quot;&gt;\n\n    &lt;!-- your content layout --&gt;\n\n    &lt;android.support.design.widget.NavigationView\n            android:layout_width=&quot;wrap_content&quot;\n            android:layout_height=&quot;match_parent&quot;\n            android:layout_gravity=&quot;start&quot;\n            android:id=&quot;@+id\/nav_view&quot;\n            app:headerLayout=&quot;@layout\/drawer_header&quot;\n            app:menu=&quot;@menu\/nav_menu&quot;\/&gt;\n&lt;\/android.support.v4.widget.DrawerLayout&gt;\n<\/pre>\n<h3>Add the Menu<\/h3>\n<p>You should note two attributes for NavigationView: <b>app:headerLayout<\/b> controls the (optional) layout used for the header and <b>app:menu<\/b> is the menu resource inflated for the navigation items. Create a new file named <b>nav_menu.xml<\/b> under <b>Resources\/menu<\/b>. <\/p>\n<p>A simple drawer will just contain a few items, but there&#8217;s nothing preventing you from having deeper menu hierarchies with sub-headers, like in the screenshot:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;menu xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;&gt;\n  &lt;group android:checkableBehavior=&quot;single&quot;&gt;\n    &lt;item\n      android:id=&quot;@+id\/nav_home&quot;\n      android:icon=&quot;@drawable\/ic_dashboard&quot;\n      android:title=&quot;Home&quot; \/&gt;\n    &lt;item\n      android:id=&quot;@+id\/nav_messages&quot;\n      android:icon=&quot;@drawable\/ic_event&quot;\n      android:title=&quot;Messages&quot; \/&gt;\n    &lt;item\n      android:id=&quot;@+id\/nav_friends&quot;\n      android:icon=&quot;@drawable\/ic_headset&quot;\n      android:title=&quot;Friends&quot; \/&gt;\n    &lt;item\n      android:id=&quot;@+id\/nav_discussion&quot;\n      android:icon=&quot;@drawable\/ic_forum&quot;\n      android:title=&quot;Discussion&quot; \/&gt;\n  &lt;\/group&gt;\n  &lt;item android:title=&quot;Sub items&quot;&gt;\n    &lt;menu&gt;\n      &lt;item\n        android:icon=&quot;@drawable\/ic_dashboard&quot;\n        android:title=&quot;Sub item 1&quot; \/&gt;\n      &lt;item\n        android:icon=&quot;@drawable\/ic_forum&quot;\n        android:title=&quot;Sub item 2&quot; \/&gt;\n     &lt;\/menu&gt;\n  &lt;\/item&gt;\n&lt;\/menu&gt;\n<\/pre>\n<h3>Code Behind Setup<\/h3>\n<p>You&#8217;re nearly done! All you have to do now is fill in a little bit of code behind to cue click events to open and close the drawer and react when a menu item is selected. You&#8217;ll need to locate the DrawerLayout and NavigationView in OnCreate for your Activity, and then add an event handler to the NavigationView&#8217;s NavigationItemSelected event. This is triggered when an item is selected, and you will need to set it to &#8220;checked&#8221; and close the drawer.<\/p>\n<pre class=\"lang:csharp decode:true\">\nDrawerLayout drawerLayout;\nNavigationView navigationView;\nprotected override void OnCreate (Bundle bundle)\n{\n  base.OnCreate (bundle);\n\n  \/\/ Set our view from the &quot;main&quot; layout resource\n  SetContentView (Resource.Layout.main_layout);\n  var toolbar = FindViewById&lt;Android.Support.V7.Widget.Toolbar&gt;(Resource.Id.toolbar);\n  SetSupportActionBar (toolbar);\n\n  \/\/Enable support action bar to display hamburger\n  SupportActionBar.SetHomeAsUpIndicator (Resource.Drawable.ic_menu);\n  SupportActionBar.SetDisplayHomeAsUpEnabled (true);\n\t\t\t\n  drawerLayout = FindViewById&lt;DrawerLayout&gt; (Resource.Id.drawer_layout);\n  navigationView = FindViewById&lt;NavigationView&gt; (Resource.Id.nav_view);\n\n  navigationView.NavigationItemSelected += (sender, e) =&gt; {\n    e.MenuItem.SetChecked (true);\n    \/\/react to click here and swap fragments or navigate\n    drawerLayout.CloseDrawers ();\n  };\n}\n<\/pre>\n<p>The only other thing to do now is to tell the drawer layout to open the drawer if the hamburger button is pressed:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic override bool OnOptionsItemSelected (IMenuItem item) \n{\n  switch (item.ItemId) \n  {\n    case Android.Resource.Id.Home:\n      drawerLayout.OpenDrawer (Android.Support.V4.View.GravityCompat.Start);\n      return true;\n  }\n  return base.OnOptionsItemSelected (item);\n}\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/NavigationDrawerInAction.gif\" alt=\"Navigation Drawer In Action in Android Application\" width=\"210\" height=\"360\" class=\"aligncenter size-full wp-image-19814\" \/><\/p>\n<h2>So Much More Material<\/h2>\n<p>This is only the start of your Material Design adventure. Be sure to check out the <a href=\"https:\/\/components.xamarin.com\/gettingstarted\/xamandroidsupportdesign\" title=\"Getting Started Guide\">Support Design Library&#8217;s Getting Started Guide<\/a> on how to take advantage of native Floating Action Button, Material Tabs, Collapsing Toolbars, and complex animations with CoordinatorLayout.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It was clear when Google introduced Material Design that it would shift the Android landscape even more than its Holo predecessor, and thus far the journey for developers to implement those new design principles in their apps has been a bit of a long and bumpy ride. In the beginning, Material Design was only available [&hellip;]<\/p>\n","protected":false},"author":544,"featured_media":19815,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5,4],"class_list":["post-19806","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-android","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>It was clear when Google introduced Material Design that it would shift the Android landscape even more than its Holo predecessor, and thus far the journey for developers to implement those new design principles in their apps has been a bit of a long and bumpy ride. In the beginning, Material Design was only available [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/19806","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=19806"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/19806\/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=19806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=19806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=19806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}