{"id":16468,"date":"2015-01-28T12:30:17","date_gmt":"2015-01-28T17:30:17","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=16468"},"modified":"2015-01-28T12:30:17","modified_gmt":"2015-01-28T17:30:17","slug":"android-tips-hello-material-design-v7-appcompat","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/android-tips-hello-material-design-v7-appcompat\/","title":{"rendered":"Android Tips: Hello Material Design v7 AppCompat"},"content":{"rendered":"<p>\t\t\t\t<img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Nexus-4-KitKat-Screentshot-2-614x1024.png\" alt=\"Nexus 4 (KitKat) Screentshot 2\" width=\"200\" class=\"alignright size-large wp-image-16472\" \/>Android 5.0 Lollipop introduced a complete refresh, known as Material Design, to the Android user interface, which generated rapid adoption throughout the development community. Until Google introduced revision 21 of the <a href=\"https:\/\/components.xamarin.com\/view\/xamandroidsupportv7appcompat\" title=\"AppCompat v7 Component\">Android Support v7 AppCompat  library<\/a>, Material Design themes, controls, and features were utilized only if you wanted to target devices running API 21.<\/p>\n<p>We recently <a href=\"\/android-support-library-v7-hello-actionbarcompat\/\" title=\"Adding Support Library v7 in Xamarin\">took a look at<\/a> how this library gives developers an opportunity to add the ActionBar to applications targeting older devices all the way back to API 7. However, now you can use this updated library to add Material Design to any of your applications and use new controls like the <a href=\"\/android-tips-hello-toolbar-goodbye-action-bar\/\" title=\"Using the Android Toolbar in Xamarin\">new Toolbar, which replaces the ActionBar<\/a>. Let&#8217;s see how easy it is to get started with the new Support Library v7 AppCompat by updating our Toolbar example.<\/p>\n<h2>Getting Started<\/h2>\n<p>To start integrating AppCompat into your Android project you will want to install the <a href=\"http:\/\/components.xamarin.com\/view\/xamandroidsupportv7appcompat\" title=\"Support Library v7 AppCompat Component\">Support Library v7 AppCompat from the Component Store<\/a> or directly from <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.v7.AppCompat\/\" title=\"Support Library v7 AppCompat NuGet\">NuGet<\/a>. This will also install the Support v4 Library, which is required because it provides controls that help maintain backwards compatibility. <\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Supportv7Component-1024x301.png\" alt=\"Supportv7Component\" width=\"1024\" height=\"301\" class=\"aligncenter size-large wp-image-16469\" \/><\/p>\n<p>Since you will now be able to target older versions of Android, you will want to adjust your Minimum and Target Android version under Android Application in the project options.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/MinandTarger-1024x331.png\" alt=\"MinandTarget\" width=\"1024\" height=\"331\" class=\"aligncenter size-large wp-image-16470\" \/><\/p>\n<h2>Updating to Material Theme<\/h2>\n<p>There are many differences to the actual resources that are embedded into your app when you use the AppCompat library. This means that the actual themes and attributes are completely different between normal Material and AppCompat themes. You will find three new themes that you can inherit from:<\/p>\n<p><strong>Dark<\/strong>: Theme.AppCompat\n<strong>Light<\/strong>: Theme.AppCompat.Light\n<strong>Light with Dark ActionBar<\/strong>: Theme.AppCompat.Light.DarkActionBar<\/p>\n<p><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/MaterialDesign.png\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/MaterialDesign-1024x559.png\" alt=\"MaterialDesign\" width=\"500\" class=\"alignnone size-large wp-image-16471\" \/><\/a><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/StylesFolders-300x146.png\" alt=\"StylesFolders\" width=\"200\" class=\"alignright size-medium wp-image-16476\" \/>To use one of these themes, you&#8217;ll want to create two folders under your Resources folder named <code>values<\/code> and <code>values-v21<\/code> where you will create a <code>styles.xml<\/code> where you can customize your applications theme. In our values\/styles.xml you will create the base theme of your application and then set custom attributes. You will see that these attributes are different than using the standard Material Theme since they have dropped the <b>android:<\/b> prefix as they are now part of your application.<\/p>\n<p><b>values\/styles.xml<\/b><\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;resources&gt;\n  &lt;style name=&quot;MyTheme&quot; parent=&quot;MyTheme.Base&quot;&gt;\n  &lt;\/style&gt;\n  &lt;!-- Base theme applied no matter what API --&gt;\n  &lt;style name=&quot;MyTheme.Base&quot; parent=&quot;Theme.AppCompat.Light.DarkActionBar&quot;&gt;\n    &lt;!--If you are using revision 22.1 please use just windowNoTitle. Without android:--&gt;\n    &lt;item name=&quot;windowNoTitle&quot;&gt;true&lt;\/item&gt;\n    &lt;!--We will be using the toolbar so no need to show ActionBar--&gt;\n    &lt;item name=&quot;windowActionBar&quot;&gt;false&lt;\/item&gt;\n    &lt;!-- Set theme colors from http:\/\/www.google.com\/design\/spec\/style\/color.html#color-color-palette--&gt;\n    &lt;!-- colorPrimary is used for the default action bar background --&gt;\n    &lt;item name=&quot;colorPrimary&quot;&gt;#2196F3&lt;\/item&gt;\n    &lt;!-- colorPrimaryDark is used for the status bar --&gt;\n    &lt;item name=&quot;colorPrimaryDark&quot;&gt;#1976D2&lt;\/item&gt;\n    &lt;!-- colorAccent is used as the default value for colorControlActivated\n         which is used to tint widgets --&gt;\n    &lt;item name=&quot;colorAccent&quot;&gt;#FF4081&lt;\/item&gt;\n     &lt;!-- You can also set colorControlNormal, colorControlActivated\n         colorControlHighlight and colorSwitchThumbNormal. --&gt;\n  &lt;\/style&gt;\n&lt;\/resources&gt;\n<\/pre>\n<p>Then, inside of your <code>values-v21\/styles.xml<\/code>, you&#8217;ll set a few more custom attributes that can take advantage of transitions on devices running Lollipop:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;resources&gt;\n  &lt;!--\n        Base application theme for API 21+. This theme replaces\n        MyTheme from resources\/values\/styles.xml on API 21+ devices.\n    --&gt;\n  &lt;style name=&quot;MyTheme&quot; parent=&quot;MyTheme.Base&quot;&gt;\n    &lt;item name=&quot;android:windowContentTransitions&quot;&gt;true&lt;\/item&gt;\n    &lt;item name=&quot;android:windowAllowEnterTransitionOverlap&quot;&gt;true&lt;\/item&gt;\n    &lt;item name=&quot;android:windowAllowReturnTransitionOverlap&quot;&gt;true&lt;\/item&gt;\n    &lt;item name=&quot;android:windowSharedElementEnterTransition&quot;&gt;@android:transition\/move&lt;\/item&gt;\n    &lt;item name=&quot;android:windowSharedElementExitTransition&quot;&gt;@android:transition\/move&lt;\/item&gt;\n  &lt;\/style&gt;\n&lt;\/resources&gt;\n<\/pre>\n<p>The last step is to open your AndroidManifest.xml under the Properties folder and add reference to your new theme under the application node with <code>android:theme=\"@style\/MyTheme\"<\/code>.<\/p>\n<h2>Integrate Toolbar<\/h2>\n<p>Previously, we saw <a href=\"https:\/\/blog.xamarin.com\/android-tips-hello-toolbar-goodbye-action-bar\/\" title=\"Integrating the new Toolbar\">how to integrate the new Toolbar<\/a> into your application. The core of that remains the same, except that you will now directly reference the new Toolbar widget from the Support Library. If you haven&#8217;t already create a new Android Layout file under Resources\/Layout\/toolbar.axml:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;android.support.v7.widget.Toolbar \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:id=&quot;@+id\/toolbar&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;wrap_content&quot;\n    android:minHeight=&quot;?attr\/actionBarSize&quot;\n    android:background=&quot;?attr\/colorPrimary&quot;\n    android:theme=&quot;@style\/ThemeOverlay.AppCompat.Dark.ActionBar&quot;\n    app:popupTheme=&quot;@style\/ThemeOverlay.AppCompat.Light&quot; \/&gt;\n<\/pre>\n<p>Note here the use of custom themes directly for the Toolbar and the use of custom attributes. These are a change form the normal use of the Toolbar when not using AppCompat. With our Toolbar setup we can now integrate it into our layouts by user the <code>include<\/code> node to place the Toolbar where you want.<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;RelativeLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    xmlns:local=&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot;\n    android:layout_width=&quot;fill_parent&quot;\n    android:layout_height=&quot;fill_parent&quot;&gt;\n    &lt;include\n        android:id=&quot;@+id\/toolbar&quot;\n        layout=&quot;@layout\/toolbar&quot; \/&gt;\n    &lt;LinearLayout\n        android:orientation=&quot;vertical&quot;\n        android:layout_width=&quot;fill_parent&quot;\n        android:layout_height=&quot;fill_parent&quot;\n        android:id=&quot;@+id\/main_content&quot;\n        android:layout_below=&quot;@id\/toolbar&quot;&gt;\n    &lt;\/LinearLayout&gt;\n&lt;\/RelativeLayout&gt;\n<\/pre>\n<h2>Update that Activity<\/h2>\n<p>The core of any Android application are Activities. When you start using AppCompat, you simply need to update your activities inheritance.\nFrom:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic class MainActivity : Activity\n<\/pre>\n<p>To:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic class MainActivity : ActionBarActivity\n<\/pre>\n<p>You can now setup your Activity to set the SupportActionBar with the new Toolbar that you have embedded into your layout. I prefer to use an alias to ensure the correct Toolbar is always referenced:<\/p>\n<pre class=\"lang:csharp decode:true\">\nusing Toolbar = Android.Support.V7.Widget.Toolbar;\n<\/pre>\n<p>Then you simply need to find the Toolbar, set it, and access it in your <code>OnCreate<\/code>:<\/p>\n<pre class=\"lang:csharp decode:true\">\nprotected override void OnCreate (Bundle bundle)\n{\n  base.OnCreate (bundle);\n  \/\/ Set our view from the &quot;main&quot; layout resource\n  SetContentView (Resource.Layout.main);\n\n  var toolbar = FindViewById&lt;Toolbar&gt; (Resource.Id.toolbar);\n\n  \/\/Toolbar will now take on default actionbar characteristics\n  SetSupportActionBar (toolbar);\n\n  SupportActionBar.Title = &quot;Hello from Appcompat Toolbar&quot;;\n  \/\/..\n}\n<\/pre>\n<p>There you have it, you can now run your newly Material Themed Android application on devices that aren&#8217;t running Lollipop!<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Nexus-4-KitKat-Screentshot-1-614x1024.png\" alt=\"Nexus 4 (KitKat) Screentshot 1\" width=\"200\" class=\"aligncenter size-large wp-image-16473\" \/><\/p>\n<h2>Learn More<\/h2>\n<p>You can get the full source code for this project in the <a href=\"http:\/\/developer.xamarin.com\/samples\/android\/Support%20v7\/\" title=\"Support v7 Sample Gallery\">Support v7 Sample gallery<\/a>. There you will also find other awesome examples of more Support v7 libraries such as CardView, RecyclerView, and Palette. During Evolve 2014, J\u00e9r\u00e9mie Laval and I gave two talks on Material Design from Concept to Implementation. (<a href=\"https:\/\/www.youtube.com\/watch?x-yt-ts=1421828030&amp;x-yt-cl=84411374&amp;v=A1WKe08uxd4\" title=\"Evolve 2014: Material Design Part 1\">Part 1<\/a> and <a href=\"https:\/\/www.youtube.com\/watch?v=_q9lorDZn0Y\" title=\"Evolve 2014: Material Android Design Part 2\">Part 2<\/a>) that are available to watch now.  <\/p>\n<p><a href=\"http:\/\/forums.xamarin.com\/discussion\/31790\" title=\"Discuss on Xamarin Forums\" target=\"_blank\"><em>Discuss this post in the Xamarin forums.<\/em><\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android 5.0 Lollipop introduced a complete refresh, known as Material Design, to the Android user interface, which generated rapid adoption throughout the development community. Until Google introduced revision 21 of the Android Support v7 AppCompat library, Material Design themes, controls, and features were utilized only if you wanted to target devices running API 21. We [&hellip;]<\/p>\n","protected":false},"author":544,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-16468","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Android 5.0 Lollipop introduced a complete refresh, known as Material Design, to the Android user interface, which generated rapid adoption throughout the development community. Until Google introduced revision 21 of the Android Support v7 AppCompat library, Material Design themes, controls, and features were utilized only if you wanted to target devices running API 21. We [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/16468","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=16468"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/16468\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=16468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=16468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=16468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}