{"id":8658,"date":"2013-12-11T11:00:19","date_gmt":"2013-12-11T16:00:19","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=8658"},"modified":"2013-12-11T11:00:19","modified_gmt":"2013-12-11T16:00:19","slug":"android-support-library-v7-hello-actionbarcompat","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/android-support-library-v7-hello-actionbarcompat\/","title":{"rendered":"Android Support Library v7: Hello ActionBarCompat"},"content":{"rendered":"<p>\t\t\t\tTo provide backwards compatibility for new features and controls in Android, Google regularly releases a set of support libraries that developers can integrate into their application. You might already be very familiar with <a href=\"http:\/\/docs.xamarin.com\/guides\/android\/platform_features\/fragments\/part_4_-_providing_backwards_compatibility_with_the_android_support_package\/\" title=\"Support Library v4\" target=\"_blank\">Support Library v4<\/a>, which gives developers that want to provide support all the way back to Android 1.6 (v4) with features such as Fragments, ViewPager, DrawerLayout, and so much more. One lacking feature of the v4 Library was the use of the Action Bar, which there have been a few libraries providing backwards compatibility for, such as <a href=\"https:\/\/components.xamarin.com\/view\/legacybar\" title=\"LegacyBar Component\" target=\"_blank\">LegacyBar<\/a> and <a href=\"https:\/\/components.xamarin.com\/view\/XamarinActionBarSherlock\" title=\"ActionBarSherlock Component\" target=\"_blank\">ActionBarSherlock<\/a>. While these both provide great Action Bar replacement for targeting pre-HoneyComb devices, Google has recently released Support Library v7, now available on the <a href=\"https:\/\/components.xamarin.com\/view\/xamandroidsupportv7appcompat\" title=\"Component Store v7 ActionBar Compat\" target=\"_blank\">Component Store<\/a>, which adds a few nice features including ActionBarCompat. The only requirement is that your application must have a minimum target of Android 2.1 (v7) to use the Support Library.<\/p>\n<p>Recently, Support Library v7 AppCompat has been updated for material design. See our <a href=\"\/android-tips-hello-material-design-v7-appcompat\/\" title=\"Material Design AppCompat\">full blog post on how to integrate AppComps&#8217;s latest version<\/a> add material design and the new Toolbar to your app.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/ActionBar.png\" alt=\"ActionBar\" width=\"956\" height=\"170\" class=\"aligncenter size-full wp-image-8659\" \/><\/p>\n<p><strong>Support Library v7<\/strong> is split into multiple components including <a href=\"https:\/\/components.xamarin.com\/view\/xamandroidsupportv7appcompat\" title=\"App Compat Component\" target=\"_blank\">AppCompat<\/a>, <a href=\"https:\/\/components.xamarin.com\/view\/xamandroidsupportv7gridlayout\" title=\"GridLayout Component\" target=\"_blank\">GridLayout<\/a>, and MediaRouter (component coming soon). <a href=\"http:\/\/docs.xamarin.com\/samples\/GridLayoutDemo\/\" title=\"GridLayout Sample\" target=\"_blank\">GridLayout<\/a> allows you to create and arrange user interface elements using a grid of rows and columns similar to the Grid in Windows Store\/Phone. MediaRouter Library includes MediaRouter, MediaRouteProvider and additional media classes for Google Cast. These libraries provide functionality for controlling the routing of media channels and streams from an Android device to external screens, speakers, and other destination devices. <\/p>\n<p>By far the most exciting addition in v7 is the AppCompat library, which adds not only the ActionBar and ActionBarActivity, but also the ShareActionProfider, which allows a standardized sharing action such as sharing text or images from inside the Action Bar.<\/p>\n<h2>Getting Started:<\/h2>\n<p>To get started with Support Library v7 you will need to simply add the <a href=\"https:\/\/components.xamarin.com\/view\/xamandroidsupportv7appcompat\" title=\"Component for v7 AppCompat\" target=\"_blank\">Support Library v7 AppCompat<\/a> from the Component Store into your Android App.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/2013-11-20_1642-1024x272.png\" alt=\"Support v7\" width=\"1024\" height=\"272\" class=\"aligncenter size-large wp-image-8707\" \/><\/p>\n<p>Once you have the Support Library component added we can now adjust the minimum and target version numbers for our a\\Android application under the project properties. Since we added Support Library v7 we are able to set our minimum to API 7 and set the target to the current android API version of 18.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/MinimumVersion-1024x107.png\" alt=\"MinimumVersion\" width=\"1024\" height=\"107\" class=\"aligncenter size-large wp-image-9640\" \/><\/p>\n<p>Adding an Action Bar to your application is now as easy inheriting from &#8220;<strong>ActionBarActivity<\/strong>&#8221; instead of &#8220;<strong>Activity<\/strong>&#8220;:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic class MainActivity : ActionBarActivity\n<\/pre>\n<p>Styling your Action Bar in AppCompat is a little bit different as now there are unified themes including:<\/p>\n<p><strong>Holo<\/strong>: @style\/Theme.AppCompat\n<strong>Holo Light<\/strong>: @style\/Theme.AppCompat.Light\n<strong>Holo Light with Dark Action Bar<\/strong>: @style\/Theme.AppCompat.Light.DarkActionBar<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/DarkTheme.png\" alt=\"DarkTheme\" width=\"958\" height=\"192\" class=\"aligncenter size-full wp-image-8660\" \/><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/LightTheme.png\" alt=\"LightTheme\" width=\"962\" height=\"176\" class=\"aligncenter size-full wp-image-8661\" \/><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/DarkLightTheme.png\" alt=\"DarkLightTheme\" width=\"958\" height=\"182\" class=\"aligncenter size-full wp-image-8662\" \/><\/p>\n<p>You can set these themes in the attributes when creating your Activity:<\/p>\n<pre class=\"lang:csharp decode:true\">\n[Activity (Label = &quot;ActionBarCompat&quot;, Icon = &quot;@drawable\/ic_launcher&quot;, Theme = &quot;@style\/Theme.AppCompat.Light&quot;, MainLauncher = true)]\n<\/pre>\n<h2>Adding Actions:<\/h2>\n<p>Android Action Bar items are defined in your Menu xml files. If you have created menus before you can simply update your xml files as there are a few key differences. First, is that you will want to update your icons so they are the correct size for the Action Bar. You can use Google\u2019s Android Asset Studio to generate these new icons. Second, is that there are new specific attributes on the menu items that define Action Bar specific functionality such as \u201cshowAsAction\u201d, which is used to determine which items are always in the Action Bar or if they are in the over flow.  Here is an example of setting this:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;menu xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n      xmlns:app=&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot; &gt;\n    &lt;item android:id=&quot;@+id\/action_edit&quot;\n          android:title=&quot;@string\/edit&quot;\n          android:icon=&quot;@drawable\/ic_action_content_edit&quot;\n          app:showAsAction=&quot;ifRoom&quot;\/&gt;\n  &lt;item android:id=&quot;@+id\/action_save&quot;\n          android:title=&quot;@string\/save&quot;\n          android:icon=&quot;@drawable\/ic_action_content_save&quot;\n          app:showAsAction=&quot;always&quot;\/&gt;\n&lt;\/menu&gt;\n<\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/ActionBarItems.png\" alt=\"ActionBarItems\" width=\"960\" height=\"180\" class=\"aligncenter size-full wp-image-8663\" \/><\/p>\n<p>The most important part here is that I added a custom \u201cxmlns\u201d definition of xmlns:app=&#8221;http:\/\/schemas.android.com\/apk\/res-auto&#8221; which is used by Android to find where the property is located when we use it in \u201capp:showAsAction\u201d.<\/p>\n<h2>Share Action Providers:<\/h2>\n<p>One great native feature is the <strong>ShareActionProvider<\/strong>, which allows you to add an item to the Action Bar that will display a list of applications that can handle a specific share intent, such as sharing text or images. <\/p>\n<p>You can add this to your Menu xml by specifying the <strong>actionProviderClass<\/strong> attribute:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;item android:id=&quot;@+id\/action_share&quot;\n          android:title=&quot;@string\/share&quot;\n          app:showAsAction=&quot;always&quot;\n          app:actionProviderClass=&quot;android.support.v7.widget.ShareActionProvider&quot;\n          \/&gt;\n<\/pre>\n<p>We will then get reference to this <strong>ShareActionProvider<\/strong> when we create the menu and specify the Intent for it to use:<\/p>\n<pre class=\"lang:csharp decode:true\">\nShareActionProvider actionProvider;\npublic override bool OnCreateOptionsMenu(IMenu menu)\n{\n  this.MenuInflater.Inflate(Resource.Menu.main_menu, menu);\n\n  var shareItem = menu.FindItem(Resource.Id.action_share);\n  var test = MenuItemCompat.GetActionProvider (shareItem);\n  actionProvider = test.JavaCast&lt;ShareActionProvider&gt;();\n\n  var intent = new Intent(Intent.ActionSend);\n  intent.SetType(&quot;text\/plain&quot;);\n  intent.PutExtra(Intent.ExtraText, &quot;ActionBarCompat is Awesome! Support Lib v7 #Xamarin&quot;);\n\n  actionProvider.SetShareIntent (intent);\n\n  return base.OnCreateOptionsMenu(menu);\n}\n<\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/ShareIntent.png\" alt=\"ShareIntent\" width=\"960\" height=\"746\" class=\"aligncenter size-full wp-image-8664\" \/><\/p>\n<h2>ProgressBar in Action Bar:<\/h2>\n<p>Showing a spinning indeterminate ProgressBar is a great way of notifying your users that you are loading information into your view. You are now able to add this functionality into the ActionBarCompat with just a few lines of code. You will have to request the WindowFeatures.IndeterminateProgress feature and then set the visibility of the ProgressBar<\/p>\n<p>Here I have setup a button that will toggle the <strong>ProgressBar<\/strong> on and off:<\/p>\n<pre class=\"lang:csharp decode:true\">\nbool indeterminateVisible;\nprotected override void OnCreate (Bundle bundle)\n{\n  base.OnCreate (bundle);\n\n  \/\/ Needs to be called before setting the content view\n  SupportRequestWindowFeature((int)WindowFeatures.IndeterminateProgress);\n\n  \/\/ Set our view from the &quot;main&quot; layout resource\n  SetContentView (Resource.Layout.Main);\n\n  SetSupportProgressBarIndeterminate (true);\n\n  var button = FindViewById&lt;Button&gt; (Resource.Id.progress_button);\n  button.Click += (sender, e) =&gt; {\n    \/\/ Switch the state of the ProgressBar and set it\n    indeterminateVisible = !indeterminateVisible;\n    SetSupportProgressBarIndeterminateVisibility(indeterminateVisible);\n    \/\/ Update the button text\n    button.Text = indeterminateVisible ? &quot;Stop Progress&quot; : &quot;Start Progress&quot;;\n   };\n}\n<\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/ProgressBar.png\" alt=\"ProgressBar\" width=\"956\" height=\"170\" class=\"aligncenter size-full wp-image-8665\" \/><\/p>\n<p>This is just a peek at some of the features of Support Library v7. After you have <a href=\"https:\/\/components.xamarin.com\/view\/xamandroidsupportv7appcompat\" title=\"v7 Components\" target=\"_blank\">installed the components<\/a> make sure that you read the documentation and the sample application showing off a lot more features. For a full sample of the code here feel free to <a href=\"https:\/\/github.com\/jamesmontemagno\/ActionBarCompat\" title=\"ActionBarCompat Sample\" target=\"_blank\">download it from GitHub<\/a> and get ActionBarCompat integrated into your app today!<\/p>\n<p><a href=\"http:\/\/forums.xamarin.com\/discussion\/11217\" title=\"Discuss in Forums\" target=\"_blank\"><em>Discuss this blog post in the Xamarin Forums<\/em><\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To provide backwards compatibility for new features and controls in Android, Google regularly releases a set of support libraries that developers can integrate into their application. You might already be very familiar with Support Library v4, which gives developers that want to provide support all the way back to Android 1.6 (v4) with features such [&hellip;]<\/p>\n","protected":false},"author":544,"featured_media":39167,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5,4],"class_list":["post-8658","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-android","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>To provide backwards compatibility for new features and controls in Android, Google regularly releases a set of support libraries that developers can integrate into their application. You might already be very familiar with Support Library v4, which gives developers that want to provide support all the way back to Android 1.6 (v4) with features such [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/8658","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=8658"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/8658\/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=8658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=8658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=8658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}