{"id":33429,"date":"2017-10-02T12:25:39","date_gmt":"2017-10-02T19:25:39","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=33429"},"modified":"2017-10-02T12:25:39","modified_gmt":"2017-10-02T19:25:39","slug":"android-support-library-v26-overview","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/android-support-library-v26-overview\/","title":{"rendered":"Android Support Library v26 Overview"},"content":{"rendered":"<p>\t\t\t\tThe Android Support Library v26 brings lots of new features to our Android apps, including fonts as resources, downloadable fonts, emoji support, an autosizing TextView, physics driven animations via Spring and Fling, backwards compatibility for animated vectors, and a slimmer library by increasing the <code>minSdkVersion<\/code> to 14. If it isn&#8217;t already, the <code>minSdkVersion<\/code> for all of your apps should now be set to 14.<\/p>\n<p>If you aren&#8217;t familiar with the Android Support Library, you&#8217;re required to compile against the same Android API level as the support library version. In other words, make sure that you have your <code>&lt;TargetFramework&gt;<\/code> set to 8.0 (API 26) to compile against the Android Support Library v26. To get support for Android 8.0, you can follow installation instructions here:<\/p>\n<ul>\n<li>Visual Studio 2017 version 15.4.0 Preview 3: <a href=\"https:\/\/www.visualstudio.com\/vs\/preview\/\" target=\"_blank\" rel=\"noopener noreferrer\">Visual Studio Preview Installer<\/a><\/li>\n<li>Visual Studio 2017 for Mac: <a href=\"https:\/\/docs.microsoft.com\/visualstudio\/mac\/update\" target=\"_blank\" rel=\"noopener noreferrer\">Beta updater channel<\/a><\/li>\n<li>Visual Studio 2015 Tools for Xamarin: <a href=\"https:\/\/developer.xamarin.com\/recipes\/cross-platform\/ide\/change_updates_channel\/#visualstudio\" target=\"_blank\" rel=\"noopener noreferrer\">Beta updater channel<\/a><\/li>\n<\/ul>\n<h2 id=\"fontsupportinxml\">Font support in XML<\/h2>\n<p>You can now put fonts in a new <code>font<\/code> resource folder. Use <code>Resources.GetFont<\/code> or <code>ResourcesCompat.GetFont<\/code> to load font resources in your application.<\/p>\n<p><em>e.g. Defining a font in XML in the Resources\/font folder:<\/em><\/p>\n<pre class=\"lang:xml decode:true\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;font-family xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"&gt;\n    &lt;font\n        android:fontStyle=\"normal\"\n        android:fontWeight=\"400\"\n        android:font=\"@font\/lobster_regular\" \/&gt;\n    &lt;font\n        android:fontStyle=\"italic\"\n        android:fontWeight=\"400\"\n        android:font=\"@font\/lobster_italic\" \/&gt;\n&lt;\/font-family&gt;<\/pre>\n<p>Using a font resource in a <code>View<\/code>:<\/p>\n<pre class=\"lang:xml decode:true\">&lt;TextView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:fontFamily=\"@font\/lobster\"\/&gt;<\/pre>\n<p><strong>NuGet:<\/strong><\/p>\n<p><a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.v4\/26.0.2-beta1\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.v4\/26.0.2-beta1<\/a><\/p>\n<p><strong>Requires:<\/strong><\/p>\n<p>API 14+<\/p>\n<h2 id=\"downloadablefonts\">Downloadable Fonts<\/h2>\n<p>There is a new <code>FontsContractCompat<\/code> class that allows you to request fonts from a font provider instead of bundling them inside your application. You can use the font provider through Google Fonts (800+ fonts).<\/p>\n<p>To use this, first create a <code>FontRequest<\/code>:<\/p>\n<pre class=\"lang:xml decode:true\">FontRequest request = new FontRequest(\n    \"com.google.android.gms.fonts\",\n    \"com.google.android.gms\",\n    query,\n    Resource.Array.com_google_android_gms_fonts_certs);<\/pre>\n<p>Second, you need to register a <code>FontRequestCallback<\/code> which implements <code>OnTypefaceRetrieved(Android.Graphics.Typeface typeface)<\/code> and <code>OnTypefaceRequestFailed(int reason)<\/code>. We&#8217;ve created one that you can explore in this example:<\/p>\n<pre class=\"lang:c# decode:true\">FontsContractCompat.FontRequestCallback callback = new FontRequestCallbackImpl\n{\n    mActivity = this,\n    mDownloadableFontTextView = DownloadableFontTextView,\n    mRequestDownloadButton = RequestDownloadButton,\n    mProgressBar = progressBar\n};<\/pre>\n<p>Finally, you need to request the font:<\/p>\n<p><code>FontsContractCompat.RequestFont(this, request, callback, GetHandlerThreadHandler());<\/code><\/p>\n<p>You can also directly request a font in XML:<\/p>\n<pre class=\"lang:xml decode:true\">&lt;font-family xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:fontProviderAuthority=\"com.google.android.gms.fonts\"\n    android:fontProviderPackage=\"com.google.android.gms\"\n    android:fontProviderQuery=\"Lobster Two\"\n    android:fontProviderCerts=\"@array\/com_google_android_gms_fonts_certs\" \/&gt;<\/pre>\n<p><strong>NuGet:<\/strong><\/p>\n<p><a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.v4\/26.0.2-beta1\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.v4\/26.0.2-beta1<\/a><\/p>\n<p><strong>Sample Project:<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/xamarin\/monodroid-samples\/tree\/master\/android-o\/DownloadableFonts\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/xamarin\/monodroid-samples\/tree\/android-o\/android-o\/DownloadableFonts<\/a><\/p>\n<p><strong>Requires:<\/strong><\/p>\n<p>API 14+<\/p>\n<p>Google Play Services 11+<\/p>\n<h2 id=\"emojicompatibility\">Emoji Compatibility<\/h2>\n<p>The <code>EmojiCompat<\/code> support library allows your devices to be up to date with the latest emoji without requiring an Android OS update. This prevents those pesky tofu characters(\u25a1) from showing up!<\/p>\n<p><code>EmojiCompat<\/code> has two main libraries: <em>downloadable<\/em> or <em>bundled<\/em>.<\/p>\n<h3 id=\"downloadable\">Downloadable<\/h3>\n<p>As outlined earlier in the <i>Downloable Fonts<\/i> section of this blog post, you first need to make a <code>FontRequest<\/code> to create an <code>FontRequestEmojiCompatConfig<\/code>.<\/p>\n<pre class=\"lang:c# decode:true\">EmojiCompat.Config config;\n\nvar fontRequest = new FontRequest(\n    \"com.google.android.gms.fonts\",\n    \"com.google.android.gms\",\n    \"Noto Color Emoji Compat\",\n    Resource.Array.com_google_android_gms_fonts_certs);\n\nconfig = new FontRequestEmojiCompatConfig(this, fontRequest)\n    .SetReplaceAll(true)\n    .RegisterInitCallback(new InitCallbackImpl());<\/pre>\n<p><strong>NuGet:<\/strong><\/p>\n<p><a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.Emoji\/26.0.2-beta1\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.Emoji\/26.0.2-beta1<\/a><\/p>\n<h3 id=\"bundled\">Bundled<\/h3>\n<p>Bundled makes life a little easier at the cost of about 7mb of a bundled font. All that&#8217;s required is that you create a <code>BundledEmojiCompatConfig<\/code>:<\/p>\n<pre class=\"lang:c# decode:true\">EmojiCompat.Config config;\n\nconfig = new BundledEmojiCompatConfig(this);<\/pre>\n<p><strong>NuGet:<\/strong><\/p>\n<p><a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.Emoji.Bundled\/26.0.2-beta1\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.Emoji.Bundled\/26.0.2-beta1<\/a><\/p>\n<h3 id=\"emojiwidgets\">Emoji Widgets<\/h3>\n<p>The <code>EmojiCompat<\/code> library provides us with three main widgets to display emojis with:<\/p>\n<p><code>EmojiTextView<\/code>, <code>EmojiEditTExt<\/code>, and <code>EmojiButton<\/code><\/p>\n<p><strong>Sample Project:<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/xamarin\/monodroid-samples\/tree\/master\/android-o\/EmojiCompat\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/xamarin\/monodroid-samples\/tree\/android-o\/android-o\/EmojiCompat<\/a><\/p>\n<p><strong>Requires:<\/strong><\/p>\n<p>API 19+<\/p>\n<h2 id=\"textviewautosizing\">TextView Autosizing<\/h2>\n<p>Your <code>TextView<\/code> will now increase text size when the container gets larger. There are three ways you can set up autosizing of <code>TextView<\/code>, explained below.<\/p>\n<h3 id=\"default\">Default<\/h3>\n<p>Start by declaring the <code>app:autoSizeTextType<\/code> to <code>uniform<\/code>.<\/p>\n<pre class=\"lang:xml decode:true\">&lt;TextView\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"200dp\"\n    app:autoSizeTextType=\"uniform\" \/&gt;<\/pre>\n<h3 id=\"granularity\">Granularity<\/h3>\n<p>You can also define a range of minimum and maximum text sizes for your\u00a0<code>TextView<\/code>. It will also increase in increments defined by your step granularity.<\/p>\n<pre class=\"lang:xml decode:true\">&lt;TextView\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"200dp\"\n    app:autoSizeTextType=\"uniform\"\n    app:autoSizeMinTextSize=\"12sp\"\n    app:autoSizeMaxTextSize=\"100sp\"\n    app:autoSizeStepGranularity=\"2sp\" \/&gt;<\/pre>\n<h3 id=\"presetsizes\">Preset Sizes<\/h3>\n<p>Finally you can specify all of the values that <code>TextView<\/code> can size to when auto-sizing. You can specify an array resource of pre-set sizes:<\/p>\n<pre class=\"lang:xml decode:true\">&lt;resources&gt;\n  &lt;array name=\"autosize_text_sizes\"&gt;\n    &lt;item&gt;10sp&lt;\/item&gt;\n    &lt;item&gt;12sp&lt;\/item&gt;\n    &lt;item&gt;20sp&lt;\/item&gt;\n    &lt;item&gt;40sp&lt;\/item&gt;\n    &lt;item&gt;100sp&lt;\/item&gt;\n  &lt;\/array&gt;\n&lt;\/resources&gt;<\/pre>\n<p>Now you just need to specify the <code>android:autoSizePresetSizes<\/code> value for the array we already created:<\/p>\n<pre class=\"lang:xml decode:true\">&lt;TextView\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"200dp\"\n    app:autoSizeTextType=\"uniform\"\n    app:autoSizePresetSizes=\"@array\/autosize_text_sizes\" \/&gt;<\/pre>\n<p><strong>NuGet:<\/strong><\/p>\n<p><a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.v4\/26.0.2-beta1\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.v4\/26.0.2-beta1<\/a><\/p>\n<p><strong>Requires:<\/strong><\/p>\n<p>API 14+<\/p>\n<h2 id=\"dynamicanimation\">Dynamic Animation<\/h2>\n<p>You can now use velocity-based animations instead of duration based ones. These animations are natural looking, with physics that mimic a fling gesture or a spring.<\/p>\n<p>To create our first dynamic animation, create a new <code>SpringAnimation<\/code> object with a <code>View<\/code>, <code>ViewProperty<\/code>, and <code>finalPosition<\/code>.<\/p>\n<pre class=\"lang:c# decode:true\">SpringAnimation animX = new SpringAnimation(box, DynamicAnimation.TranslationX, 0);\n<\/pre>\n<p>There are two main concepts that you can set the spring to: <code>Stiffness<\/code> and <code>DampingRatio<\/code>.\n<code>Stiffness<\/code> determines how fast the spring will snap back, while <code>DampingRatio<\/code> determines how bouncy the spring is.<\/p>\n<pre class=\"lang:c# decode:true\">animX.Spring.SetStiffness(Stiffness);\nanimX.Spring.SetDampingRatio(Damping);<\/pre>\n<p>You can then set your <code>StartVelocity<\/code> and <code>Start<\/code> the animation!<\/p>\n<pre class=\"lang:c# decode:true\">animX.SetStartVelocity(velocityTracker.XVelocity);\nanimX.Start();<\/pre>\n<p><img decoding=\"async\" class=\"aligncenter size-medium\" src=\"https:\/\/media.giphy.com\/media\/xT1Ra2zQ4shfeEnims\/giphy.gif\" width=\"270\" height=\"480\" \/><\/p>\n<p><strong>NuGet:<\/strong><\/p>\n<p><a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.Dynamic.Animation\/26.0.2-beta1\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.Dynamic.Animation\/26.0.2-beta1<\/a><\/p>\n<p><strong>Sample Project:<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/JonDouglas\/AndroidSupportv26Samples\/tree\/master\/SpringAnimationSample\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/JonDouglas\/AndroidSupportv26Samples\/tree\/master\/SpringAnimationSample<\/a><\/p>\n<p><strong>Requires:<\/strong><\/p>\n<p>API 14+<\/p>\n<h2 id=\"animatedvectordrawablecompat\">AnimatedVectorDrawableCompat (Bonus)<\/h2>\n<p>If you&#8217;re unaware of <code>AnimatedVectorDrawableCompat<\/code>, it&#8217;s a pretty nifty library for morphing between paths and interpolating along a path to create stunning animations, logo transitions, and more. These have all now been backported to API 14, which allows for these beautiful animated vectors to run on older devices.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-medium\" src=\"https:\/\/media.giphy.com\/media\/l0DAHoNqRJ53Hg8q4\/giphy.gif\" width=\"270\" height=\"480\" \/><\/p>\n<p>You can create your own beautiful animated vector drawables through XML by creating an <code>&lt;animated-vector&gt;<\/code> element and attaching <code>&lt;pathInterpolators&gt;<\/code> to the <code>&lt;objectAnimator&gt;<\/code> defined. If you aren&#8217;t the best animator, you can start <a href=\"http:\/\/www.androiddesignpatterns.com\/2016\/11\/introduction-to-icon-animation-techniques.html\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a> with <a href=\"https:\/\/shapeshifter.design\/\" target=\"_blank\" rel=\"noopener noreferrer\">a tool by Alex Lockwood<\/a>:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-medium\" src=\"https:\/\/media.giphy.com\/media\/xT1R9QMYeKSB6eNa00\/giphy.gif\" width=\"270\" height=\"480\" \/><\/p>\n<p><strong>NuGet:<\/strong><\/p>\n<p><a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.Animated.Vector.Drawable\/26.0.2-beta1\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.nuget.org\/packages\/Xamarin.Android.Support.Animated.Vector.Drawable\/26.0.2-beta1<\/a><\/p>\n<p><strong>Sample Project:<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/JonDouglas\/AndroidSupportv26Samples\/tree\/master\/EndlessPinJump\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/JonDouglas\/AndroidSupportv26Samples\/tree\/master\/EndlessPinJump<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/JonDouglas\/AndroidSupportv26Samples\/tree\/master\/AndroidToAppleVectorLogo\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/JonDouglas\/AndroidSupportv26Samples\/tree\/master\/AndroidToAppleVectorLogo<\/a><\/p>\n<p><strong>Requires:<\/strong><\/p>\n<p>API 14+<\/p>\n<h2>Summary<\/h2>\n<p>There are many rich features that Android ships within their support libraries that you can leverage in your applications. These features are typically backwards compatible to the <code>minSdkVersion<\/code> the support library defines. Now&#8217;s your chance to explore what the support libraries can provide for your applications!<\/p>\n<p><a href=\"https:\/\/forums.xamarin.com\/104279\/android-support-library-v26-overview\">Discuss this post on the Xamarin Forums<\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Android Support Library v26 brings lots of new features to our Android apps, including fonts as resources, downloadable fonts, emoji support, an autosizing TextView, physics driven animations via Spring and Fling, backwards compatibility for animated vectors, and a slimmer library by increasing the minSdkVersion to 14. If it isn&#8217;t already, the minSdkVersion for all [&hellip;]<\/p>\n","protected":false},"author":551,"featured_media":33436,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5,4],"class_list":["post-33429","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-android","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>The Android Support Library v26 brings lots of new features to our Android apps, including fonts as resources, downloadable fonts, emoji support, an autosizing TextView, physics driven animations via Spring and Fling, backwards compatibility for animated vectors, and a slimmer library by increasing the minSdkVersion to 14. If it isn&#8217;t already, the minSdkVersion for all [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/33429","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\/551"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=33429"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/33429\/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=33429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=33429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=33429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}