Android Support Library v26 Overview

Jon Douglas

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’t already, the minSdkVersion for all of your apps should now be set to 14.

If you aren’t familiar with the Android Support Library, you’re required to compile against the same Android API level as the support library version. In other words, make sure that you have your <TargetFramework> 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:

Font support in XML

You can now put fonts in a new font resource folder. Use Resources.GetFont or ResourcesCompat.GetFont to load font resources in your application.

e.g. Defining a font in XML in the Resources/font folder:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

Using a font resource in a View:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lobster"/>

NuGet:

https://www.nuget.org/packages/Xamarin.Android.Support.v4/26.0.2-beta1

Requires:

API 14+

Downloadable Fonts

There is a new FontsContractCompat 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).

To use this, first create a FontRequest:

FontRequest request = new FontRequest(
    "com.google.android.gms.fonts",
    "com.google.android.gms",
    query,
    Resource.Array.com_google_android_gms_fonts_certs);

Second, you need to register a FontRequestCallback which implements OnTypefaceRetrieved(Android.Graphics.Typeface typeface) and OnTypefaceRequestFailed(int reason). We’ve created one that you can explore in this example:

FontsContractCompat.FontRequestCallback callback = new FontRequestCallbackImpl
{
    mActivity = this,
    mDownloadableFontTextView = DownloadableFontTextView,
    mRequestDownloadButton = RequestDownloadButton,
    mProgressBar = progressBar
};

Finally, you need to request the font:

FontsContractCompat.RequestFont(this, request, callback, GetHandlerThreadHandler());

You can also directly request a font in XML:

<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    android:fontProviderAuthority="com.google.android.gms.fonts"
    android:fontProviderPackage="com.google.android.gms"
    android:fontProviderQuery="Lobster Two"
    android:fontProviderCerts="@array/com_google_android_gms_fonts_certs" />

NuGet:

https://www.nuget.org/packages/Xamarin.Android.Support.v4/26.0.2-beta1

Sample Project:

https://github.com/xamarin/monodroid-samples/tree/android-o/android-o/DownloadableFonts

Requires:

API 14+

Google Play Services 11+

Emoji Compatibility

The EmojiCompat 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(□) from showing up!

EmojiCompat has two main libraries: downloadable or bundled.

Downloadable

As outlined earlier in the Downloable Fonts section of this blog post, you first need to make a FontRequest to create an FontRequestEmojiCompatConfig.

EmojiCompat.Config config;

var fontRequest = new FontRequest(
    "com.google.android.gms.fonts",
    "com.google.android.gms",
    "Noto Color Emoji Compat",
    Resource.Array.com_google_android_gms_fonts_certs);

config = new FontRequestEmojiCompatConfig(this, fontRequest)
    .SetReplaceAll(true)
    .RegisterInitCallback(new InitCallbackImpl());

NuGet:

https://www.nuget.org/packages/Xamarin.Android.Support.Emoji/26.0.2-beta1

Bundled

Bundled makes life a little easier at the cost of about 7mb of a bundled font. All that’s required is that you create a BundledEmojiCompatConfig:

EmojiCompat.Config config;

config = new BundledEmojiCompatConfig(this);

NuGet:

https://www.nuget.org/packages/Xamarin.Android.Support.Emoji.Bundled/26.0.2-beta1

Emoji Widgets

The EmojiCompat library provides us with three main widgets to display emojis with:

EmojiTextView, EmojiEditTExt, and EmojiButton

Sample Project:

https://github.com/xamarin/monodroid-samples/tree/android-o/android-o/EmojiCompat

Requires:

API 19+

TextView Autosizing

Your TextView will now increase text size when the container gets larger. There are three ways you can set up autosizing of TextView, explained below.

Default

Start by declaring the app:autoSizeTextType to uniform.

<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform" />

Granularity

You can also define a range of minimum and maximum text sizes for your TextView. It will also increase in increments defined by your step granularity.

<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform"
    app:autoSizeMinTextSize="12sp"
    app:autoSizeMaxTextSize="100sp"
    app:autoSizeStepGranularity="2sp" />

Preset Sizes

Finally you can specify all of the values that TextView can size to when auto-sizing. You can specify an array resource of pre-set sizes:

<resources>
  <array name="autosize_text_sizes">
    <item>10sp</item>
    <item>12sp</item>
    <item>20sp</item>
    <item>40sp</item>
    <item>100sp</item>
  </array>
</resources>

Now you just need to specify the android:autoSizePresetSizes value for the array we already created:

<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform"
    app:autoSizePresetSizes="@array/autosize_text_sizes" />

NuGet:

https://www.nuget.org/packages/Xamarin.Android.Support.v4/26.0.2-beta1

Requires:

API 14+

Dynamic Animation

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.

To create our first dynamic animation, create a new SpringAnimation object with a View, ViewProperty, and finalPosition.

SpringAnimation animX = new SpringAnimation(box, DynamicAnimation.TranslationX, 0);

There are two main concepts that you can set the spring to: Stiffness and DampingRatio. Stiffness determines how fast the spring will snap back, while DampingRatio determines how bouncy the spring is.

animX.Spring.SetStiffness(Stiffness);
animX.Spring.SetDampingRatio(Damping);

You can then set your StartVelocity and Start the animation!

animX.SetStartVelocity(velocityTracker.XVelocity);
animX.Start();

NuGet:

https://www.nuget.org/packages/Xamarin.Android.Support.Dynamic.Animation/26.0.2-beta1

Sample Project:

https://github.com/JonDouglas/AndroidSupportv26Samples/tree/master/SpringAnimationSample

Requires:

API 14+

AnimatedVectorDrawableCompat (Bonus)

If you’re unaware of AnimatedVectorDrawableCompat, it’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.

You can create your own beautiful animated vector drawables through XML by creating an <animated-vector> element and attaching <pathInterpolators> to the <objectAnimator> defined. If you aren’t the best animator, you can start here with a tool by Alex Lockwood:

NuGet:

https://www.nuget.org/packages/Xamarin.Android.Support.Animated.Vector.Drawable/26.0.2-beta1

Sample Project:

https://github.com/JonDouglas/AndroidSupportv26Samples/tree/master/EndlessPinJump

https://github.com/JonDouglas/AndroidSupportv26Samples/tree/master/AndroidToAppleVectorLogo

Requires:

API 14+

Summary

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 minSdkVersion the support library defines. Now’s your chance to explore what the support libraries can provide for your applications!

Discuss this post on the Xamarin Forums

0 comments

Discussion is closed.

Feedback usabilla icon