{"id":15622,"date":"2014-11-25T15:06:38","date_gmt":"2014-11-25T23:06:38","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=15622"},"modified":"2014-11-25T15:06:38","modified_gmt":"2014-11-25T23:06:38","slug":"android-tips-hello-toolbar-goodbye-action-bar","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/android-tips-hello-toolbar-goodbye-action-bar\/","title":{"rendered":"Android Tips: Hello Toolbar, Goodbye Action Bar"},"content":{"rendered":"<p>\t\t\t\tIt seems like just yesterday that Google introduced the Action Bar for Android. It jump started a revolution for the Android user interface and made developer&#8217;s lives easier by providing a great way to provide menu options, tabs, and more for their users. With Android 5.0 Lollipop, all of this is about to change once again with the introduction of the <a href=\"http:\/\/developer.android.com\/reference\/android\/widget\/Toolbar.html\" title=\"Android Toolbar Documentation\" target=\"_blank\">Android Toolbar<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/toolbar.png\" alt=\"toolbar\" class=\"aligncenter size-medium wp-image-15623\" \/><\/p>\n<p>You can think of the Toolbar as the Action Bar&#8217;s bigger sibling that has grown up and is free to roam around the house by itself. It&#8217;s a generalization of the Action Bar that isn&#8217;t bound to an Activity&#8217;s window decor, can be placed anywhere in the user interface, and can even be designated as the Action Bar replacement for an Activity. It&#8217;s highly customizable, which means you can add navigation buttons, branded logos, titles, subtitle, action menu items, or even your own custom views. <\/p>\n<p>For this sample, I am going to focus on the Android 5.0 (v21) Toolbar. The same can be achieved using the new <a>Support Library v7 AppCompat<\/a>, which features a backwards compatible Toolbar and is available as a NuGet package.<\/p>\n<h2>Applying Your Theme<\/h2>\n<p>We will start by applying the Material theme to our Android application, but we will now set two special attributes <code>android:windowNoTitle<\/code> and <code>android:windowActionBar<\/code> to tell Android we will be using a Toolbar instead of the Action Bar. <\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;style name=&quot;MyTheme&quot; parent=&quot;@android:style\/Theme.Material.Light.DarkActionBar&quot;&gt;\n    &lt;item name=&quot;android: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;android: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;android:colorPrimary&quot;&gt;#2196F3&lt;\/item&gt;\n    &lt;!-- colorPrimaryDark is used for the status bar --&gt;\n    &lt;item name=&quot;android: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;android:colorAccent&quot;&gt;#FF4081&lt;\/item&gt;\n  \t&lt;!-- You can also set colorControlNormal, colorControlActivated\n         colorControlHighlight and colorSwitchThumbNormal. --&gt;\n  &lt;\/style&gt;\n<\/pre>\n<p>If you want to learn more about the new themes, be sure to read my <a href=\"\/introduction-to-android-material-design\/\" title=\"Introduction to Material Design\" target=\"_blank\">Introduction to Material Design blog post<\/a> and watch my and J\u00e9r\u00e9mie Laval&#8217;s <a href=\"https:\/\/www.youtube.com\/watch?v=A1WKe08uxd4\" title=\"Evolve 2014\" target=\"_blank\">sessions from Xamarin Evolve 2014 on Material Design<\/a>.<\/p>\n<h2>Replacing the Action Bar<\/h2>\n<p>With our theme attributes set, we can now create our first layout. The Toolbar can be found in your Android Designer&#8217;s toolbox, from which you can simply drag and drop it at the top of your layout. Often you will want to use a RelativeLayout to specify that your Toolbar is at the very top. <\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;Toolbar xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&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;?android:attr\/actionBarSize&quot;\n    android:background=&quot;?android:attr\/colorPrimary&quot; \/&gt;\n<\/pre>\n<p>Now in your <code>OnCreate<\/code> method of your activity, you will need to find your Toolbar. To have it mimic the old ActionBar, you can set a new method call <code>SetActionBar(Toolbar toolbar);<\/code>. <\/p>\n<pre class=\"lang:csharp decode:true\">\nprotected override void OnCreate (Bundle bundle)\n{\n  base.OnCreate (bundle);\n  SetContentView (Resource.Layout.Main);\n  var toolbar = FindViewById&lt;Toolbar&gt; (Resource.Id.toolbar);\n  \/\/Toolbar will now take on default Action Bar characteristics\n  SetActionBar (toolbar);\n  \/\/You can now use and reference the ActionBar\n  ActionBar.Title = &quot;Hello from Toolbar&quot;;\n}\n<\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/ToolbarNoTheme-1024x246.png\" alt=\"ToolbarNoTheme\" width=\"400\" class=\"aligncenter size-large wp-image-15632\" \/><\/p>\n<p>This is the easiest way to transition from the ActionBar to the new Toolbar, as all of your existing Action Bar menus will automatically work if you are inflating them in your <code>OnCreateOptionsMenu<\/code> method.<\/p>\n<h2>Applying Themes<\/h2>\n<p>Since the Toolbar is independent, it can also be themed independently as well. The <code>android:theme<\/code> is the main theme of the bar, and <code>android:popupTheme<\/code> is the theme to apply to menu item pop ups. If you are using a light theme, or want the look of a dark Action Bar, you can apply two themes directly on the Toolbar, which would look like this:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;Toolbar xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&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;?android:attr\/actionBarSize&quot;\n    android:background=&quot;?android:attr\/colorPrimary&quot;\n    android:theme=&quot;@android:style\/ThemeOverlay.Material.Dark.ActionBar&quot;\n    android:popupTheme=&quot;@android:style\/ThemeOverlay.Material.Light&quot; \/&gt;\n<\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/ToolbarThemedNormal.png\" alt=\"ToolbarThemedNormal\" width=\"400\" \/><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/ToolbarPopOver.png\" alt=\"ToolbarPopOver\" width=\"400\" class=\"aligncenter size-medium wp-image-15624\" \/><\/p>\n<h2>Custom Action Bars<\/h2>\n<p>As I mentioned earlier, the real reason to upgrade to the new Toolbar is that it has complete freedom to be placed where you want it and is fully customizable; it can even have a completely separate action menu that you can manage. Let&#8217;s say you want to put a Toolbar at the bottom of your screen and theme it in an accent color. You would simply add another Toolbar widget to the bottom and apply a different theme to it:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;RelativeLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    android:layout_width=&quot;fill_parent&quot;\n    android:layout_height=&quot;fill_parent&quot;&gt;\n    &lt;!--First Toolbar goes here--&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:layout_below=&quot;@id\/toolbar&quot;&gt;\n        &lt;!--Other widgets--&gt;\n        &lt;!--Custom second toolbar at the bottom with an accent color for the background.\n            Additionally, you can set any custom property and make the height larger or smaller.--&gt;\n        &lt;Toolbar\n            android:id=&quot;@+id\/toolbar_bottom&quot;\n            android:minHeight=&quot;?android:attr\/actionBarSize&quot;\n            android:background=&quot;?android:attr\/colorAccent&quot;\n            android:theme=&quot;@android:style\/ThemeOverlay.Material.Dark.ActionBar&quot;\n            android:popupTheme=&quot;@android:style\/ThemeOverlay.Material.Light&quot;\n            android:layout_width=&quot;match_parent&quot;\n            android:layout_height=&quot;wrap_content&quot; \/&gt;\n    &lt;\/LinearLayout&gt;\n&lt;\/RelativeLayout&gt;\n<\/pre>\n<p>Back in the Activity, the last step is to find this new Toolbar. Once found, we will not set it as an Action Bar, but instead we&#8217;ll inflate a separate menu and implement a new click handler when the users interact with it.<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar toolbarBottom = FindViewById&lt;Toolbar&gt; (Resource.Id.toolbar_bottom);\ntoolbarBottom.Title = &quot;Photo Editing&quot;;\ntoolbarBottom.InflateMenu (Resource.Menu.photo_edit);\n\/\/Add menu click handler\ntoolbarBottom.MenuItemClick += (sender, e) =&gt; {\t\n  Toast.MakeText(this, &quot;Bottom toolbar pressed: &quot; + e.Item.TitleFormatted, ToastLength.Short).Show();\n};\n<\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/MultipleToolbars1-615x1024.png\" alt=\"Multiple Android Toolbars\" width=\"400\" class=\"aligncenter size-large wp-image-15639\" \/><\/p>\n<h2>Going Further<\/h2>\n<p>The new Android Toolbar allows for a highly customizable developer experience that we never had access to with the standard Action Bar. You can grab the <a href=\"http:\/\/developer.xamarin.com\/samples\/monodroid\/android5.0\/Toolbar\/\" title=\"Source Code for Toolbar\">source code for this example<\/a> and try out other Android L features on our <a href=\"http:\/\/developer.xamarin.com\/samples\/android\/AndroidL\/\" title=\"Android L Sample Gallery\">Samples Gallery<\/a>. <\/p>\n<h2>Backwards Compatibility<\/h2>\n<p>Looking to add support for Material Design and the new Toolbar to your app on older versions of Android? See our full blog post on how to <a href=\"\/android-tips-hello-material-design-v7-appcompat\/\" title=\"Material Design Support v7\">add the new Android Support Library v7 AppCompat<\/a> to your Xamarin.Android app. You can find a full <a href=\"http:\/\/developer.xamarin.com\/samples\/monodroid\/Supportv7\/AppCompat\/Toolbar\/\" title=\"AppCompat Toolbar Sample\">AppCompat Toolbar sample<\/a> in our gallery.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It seems like just yesterday that Google introduced the Action Bar for Android. It jump started a revolution for the Android user interface and made developer&#8217;s lives easier by providing a great way to provide menu options, tabs, and more for their users. With Android 5.0 Lollipop, all of this is about to change once [&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":[5,4],"class_list":["post-15622","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-android","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>It seems like just yesterday that Google introduced the Action Bar for Android. It jump started a revolution for the Android user interface and made developer&#8217;s lives easier by providing a great way to provide menu options, tabs, and more for their users. With Android 5.0 Lollipop, all of this is about to change once [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/15622","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=15622"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/15622\/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=15622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=15622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=15622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}