VISUAL STUDIO   Windows Mac

Introduction to Android Oreo

PDF for offline use
Related Links:

Let us know how you feel about this

Translation Quality


0/250

last updated: 2017-09

How to get started using Xamarin.Android to develop apps for the latest version of Android.

Android 8.0 Oreo is the latest version of Android available from Google. Android Oreo offers many new features of interest to Xamarin.Android developers. These features include notification channels, notification badges, custom fonts in XML, downloadable fonts, autofill, and picture in picture (PIP). Android Oreo includes new APIs for these new capabilties, and these APIs are available to Xamarin.Android apps when you use Xamarin.Android 8.0 and later.

Android Oreo hero image

This article is structured to help you get started in developing Xamarin.Android apps for Android 8.0 Oreo. It explains how to install the necessary updates, configure the SDK, and create an emulator (or device) for testing. It also provides an outline of the new features in Android 8.0 Oreo, with links to sample apps that illustrate how to use Android Oreo features in Xamarin.Android apps.

Preview

Contents

Requirements

The following is required to use Android Oreo features in Xamarin-based apps:

  • Visual Studio – If you are using Windows, version 15.4 (Preview 2) or later of Visual Studio is required. If you are using a Mac, Visual Studio for Mac version 7.2 (build 559 or later) is required.

  • Xamarin.Android – Xamarin.Android 8.0 or later must be installed and configured with Visual Studio.

  • Android SDK – Android SDK 8.0 (API 26) or later must be installed via the Android SDK Manager.

  • If you are using Windows, the Xamarin SDK Manager must be installed.

Getting Started

To get started using Android Oreo with Xamarin.Android, you must download and install the latest tools and SDK packages before you can create an Android Oreo project:

  1. Install Xamarin updates in order to gain access to new Xamarin.Android features that support Android Oreo.

  2. Install the Android 8.0.0 (API 26) or later packages and tools via the SDK Manager.

  3. Create a new Xamarin.Android project that targets Android Oreo (API 26).

  4. Configure an emulator or device for testing Android Oreo apps.

Each of these steps is explained in the following sections:

Install Xamarin Updates

To add Xamarin support for Android Oreo, you must first apply the following updates:

  1. Set Visual Studio to a preview updates channel. For more information about how to change the updates channel, see Changing the Updates Channel.

  2. If you are using Visual Studio 2017, you must install the Xamarin Updates Extension. After this extension is installed, you can use it to install preview versions of Xamarin for Visual Studio.

  3. If you are using Visual Studio 2017, install the Xamarin Android SDK Manager (this is necessary because Google does not provide a standlone GUI SDK manager that supports API 26.0 and later). Using the SDK Manager explains how to use the Xamarin Android SDK Manager.

Install the Android SDK

To create a project with Xamarin.Android 8.0, you must first use the Xamarin Android SDK Manager to install the SDK platform for Android 8.0 - Oreo or later. You must also install Android SDK Tools 26.0 or later.

  1. Start the SDK Manager (in Visual Studio, click Tools > Android > Android SDK Manager).

  2. Install the Android 8.0 - Oreo packages. If you are using the Android SDK emulator, be sure to include the x86 system images that you will need:

    Selecting Android 8.0 packages in the Android SDK Manager

  3. Install Android SDK Tools 26.0.2 or later, Android SDK Platform-Tools 26.0.0 or later, and Android SDK Build-Tools 26.0.0 (or later):

    Selecting Android SDK Tools 26 in the Android SDK Manager

  1. Start the SDK Manager (in Visual Studio for Mac, click Tools > SDK Manager).

  2. Install the Android 8.0 - Oreo SDK packages. If you are using the Android SDK emulator, be sure to include the x86 system images that you will need:

    Selecting Android 8.0 packages in the SDK Manager

  3. Install Android SDK Tools 26.0.2 or later, Android SDK Platform-Tools 26.0.0 or later, and Android SDK Build-Tools 26.0.0 (or later):

    Selecting Android SDK Tools 26 in the SDK Manager

Start a Xamarin.Android Project

Create a new Xamarin.Android project. If you are new to Android development with Xamarin, see Hello, Android to learn about creating Xamarin.Android projects.

When you create an Android project, you must configure the version settings to target Android 8.0 or later. For example, to target your project for Android 8.0, you must configure the target Android API level of your project to Android 8.0 (API 26). It is recommended that you also set your target framework level to API 26 or later. For more about configuring Android API level levels, see Understanding Android API Levels.

Configure an Emulator or Device

If you attempt to launch the default Google GUI-based AVD Manager after installing Android SDK Tools 26.0 or later, you may get the following error dialog, which instructs you to use the command line AVD manager tool avdmanager instead:

Android Emulator Manager warning dialog

Android Emulator Manager warning dialog

This message is displayed because Google no longer provides a standlone GUI AVD manager that supports API 26.0 and later. For Android 8.0 Oreo, you must use the command-line avdmanager tool to create virtual devices for Android Oreo (a Xamarin AVD Manager will be made available soon so that these command line steps will no longer be necessary).

To use avdmanager to create a new virtual device:

  1. Open a Command Prompt window and set JAVA_HOME to the location of the Java SDK on your computer. For a typical Xamarin installation, you can use the following command:

    setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0_131"
  2. Add the location of the Android SDK bin folder to your PATH. For a typical Xamarin installation, you can use the following command:

    setx PATH "%PATH%;C:\Program Files (x86)\Android\android-sdk\tools\bin"
  3. Close the Command Prompt window and open a new Command Prompt window. Create a new virtual device by using the avdmanager command. For example, to create an AVD named AVD-Oreo-8.0 using the x86 system image for API level 26, use the following command:

    avdmanager create avd -n AVD-Oreo-8.0 -k "system-images;android-26;google_apis;x86"
  4. When you are prompted with Do you wish to create a custom hardware profile [no] you can enter no and accept the default hardware profile. If you say yes, avdmanager will prompt you with a list of questions for customizing the hardware profile.

After you use avdmanager to create your virtual device, it will be included in the device pull-down menu:

New AVD added to device pull-down menu

  1. Open a Terminal window and change to the location of the Android SDK tools directory on your Mac. For a typical Xamarin installation, you can use the following command:

    cd ~/Library/Developer/Xamarin/android-sdk-macosx/tools/bin
  2. Create a new virtual device by using the avdmanager command. For example, to create an AVD named AVD-Oreo-8.0 using the x86 system image for API level 26, use the following command:

    avdmanager create avd -n AVD-Oreo-8.0 -k "system-images;android-26;google_apis;x86"
  3. When you are prompted with Do you wish to create a custom hardware profile [no] you can enter no and accept the default hardware profile. If you say yes, avdmanager will prompt you with a list of questions for customizing the the hardware profile.

After you use avdmanager to create your virtual device, it will be included in the device pull-down menu:

New AVD added to device pull-down menu

For more information about configuring an Android emulator for testing and debugging, see Debug on an Emulator.

If you are using a physical device such as a Nexus or a Pixel, you can either update your device through automatic over the air (OTA) updates or download a system image and flash your device directly. For more information about manually updating your device to Android Oreo, see Factory Images for Nexus and Pixel Devices.

New Features

Android Oreo introduces a variety of new features and capabilities, such as notification channels, notification badges, custom fonts in XML, downloadable fonts, autofill, and picture-in-picture. The following sections highlight these features and provide links to help you get started using them in your app.

Notification Channels

Notification Channels are app-defined categories for notifications. You can create a notification channel for each type of notification that you need to send, and you can create notification channels to reflect choices made by users of your app. The new notification channels feature makes it possible for you to give users fine-grained control over different kinds of notifications. For example, if you are implementing a messaging app, you can create separate notification channels for each conversation group that is created by a user.

To create a notification channel, do the following:

  1. Construct a notification channel object with an ID that is unique within your package. To do this, you instantiate a new NotificationChannel object and pass the ID, name, and importance to the constructor:

    var importance = NotificationImportance.Default;
    string chanId = "default_channel";
    string chanName = "My Notification Channel";
    NotificationChannel chan = new NotificationChannel(chanId, chanName, importance);
  2. Configure the NotificationChannel object with initial settings. For example, the following code configures the NotificationChannel object with description text and enables vibration:

    chan.EnableVibration (true);
    chan.Description = "Test Notification channel";
  3. Submit the notification channel object to the notification manager:

    NotificationManager notificationManager =
        (NotificationManager) GetSystemService (NotificationService);
    notificationManager.CreateNotificationChannel (chan);

To post a notification to a notification channel, do the following:

  1. Configure the notification using the Notification.Builder, passing in the channel ID to the SetChannelId method. For example:

    Notification.Builder builder = new Notification.Builder(this)
        .SetContentTitle ("New message")
        .SetContentText ("My notification message")
        .SetChannelId (chanId);
  2. Build and issue the notification:

    int notifyId = 1;
    notificationManager.Notify(notifyId, builder.Build());

For a more real-world code example, see the NotificationChannels sample; this sample app manages two channels and sets additional notification options.

If you are not familiar with how to create notifications in Xamarin.Android, see Local Notifications and the Local Notifications Walkthrough. For more information about notification channels, see the Android Developer Managing notification channels topic.

Notification Badges

Notification badges are small dots that appear over app icons as shown in this screenshot:

Example notification badges on app icons

These dots indicate that there are new notifications for one or more notification channels in the app associated with that app icon – these are notifications that the user has not yet dismissed or acted upon. Users can long-press on an icon to glance at the notifications associated with a notification badge, dismissing or acting on notifications from the long-press menu that appeaars.

For more information about notification badges, see the Android Developer Notification Badges topic.

Custom Fonts in XML

Android Oreo introduces Fonts in XML, which makes it possible for you to incorporate custom fonts as resources. OpenType (.otf) and TrueType (.ttf) font formats are supported. To add fonts as resources, do the following:

  1. Create a Resources/font folder.

  2. Copy your font files (example, .ttf and .otf files) to Resources/font.

  3. If necessary, rename each font file so that it adheres to the Android file naming conventions (i.e., use only lowercase a-z, 0-9, and underscores in file names). For example, the font file Pacifico-Regular.ttf could be renamed to something like pacifico.ttf.

  4. Apply the custom font by using the new android:fontFamily attribute in your layout XML. For example, the following TextView declaration uses the added pacifico.ttf font resource:

    <TextView
      android:text="Example Text in Pacifico Regular"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:fontFamily="@font/pacifico" />

You can also create a font family XML file that describes multiple fonts as well as style and weight details. For more information, see the Android Developer Fonts in XML topic.

Downloadable Fonts

Beginning with Android Oreo, apps can request fonts from a provider rather than bundling them into the APK. Fonts are downloaded from the network only as needed. This feature reduces APK size, conserving phone memory and cellular data usage. You can also use this feature on Android API versions 14 and higher by installing the Android Support Libary 26 package.

When your app needs a font, you create a FontsRequest object (specifying the font to download) and then pass it to a FontsContract method to download the font. The following steps describe the font download process in more detail:

  1. Instantiate a FontRequest object.

  2. Subclass and instantiate FontsContract.FontRequestCallback.

  3. Implement the FontRequestCallback.OnTypeFaceRetrieved method, which is used to handle completion of the font request.

  4. Implement the FontRequestCallback.OnTypeFaceRequestFailed method, which is used to inform your app of any errors that take place during the font request process.

  5. Call the FontsContract.RequestFonts method to retrieve the font from the font provider.

When you call the RequestFonts method, it first checks to see if the font is locally cached (from a previous call to RequestFont). If it is not cached, it calls the font provider, retrieves the font asynchronously, and then passes the results back to your app by invoking your OnTypeFaceRetrieved method.

The Downloadable Fonts sample demonstrates how to use the Downloadable Fonts feature introduced in Android Oreo.

For more information about downloading fonts, see the Android Developer Downloadable Fonts topic.

Autofill

The new Autofill framework in Android Oreo makes it easier for users to handle repetitive tasks such as login, account creation, and credit card transactions. Users spend less time re-typing information (which can lead to input errors). Before your app can work with the Autofill Framework, an autofill service must be enabled in the system settings (users can enable or disable autofill).

For more information about the new Autofill feature and how to optimize your app for autofill, see the Android Developer Autofill Framework topic.

Picture in Picture (PIP)

Android Oreo makes it possible for an Activity to launch in picture-in-picture (PIP) mode, overlaying the screen of another Activity. This feature is intended for video playback.

To specify that your app's Activity can use PIP mode, set the following flag to true in the Android manifest:

android:supportsPictureInPicture

To specify how your activity should behave when it is in PIP mode, you use the new PictureInPictureParams object. PictureInPictureParams represents a set of parameters that you use to initialize and update an Activity in PIP mode (for example, the Activity's preferred aspect ratio). The following new PIP methods were added to Activity in Android Oreo:

  • EnterPictureInPictureMode – puts the Activity in PIP mode. The Activity is placed in the corner of the screen, and the rest of the screen is filled with the previous Activity that was on the screen.

  • SetPictureInPictureParams – Updates the Activity's PIP configuration settings (for example, a change in aspect ratio).

Other Features

Android Oreo contains many other new features such as the Emoji support library, Location API, background limits, wide-gamut color for apps, new audio codecs, WebView enhancements, improved keyboard navigation support, and a new AAudio (pro audio) API for high-performance low-latency audio, For more information about these features, see the Android Developer Android Oreo Features and APIs topic.

Behavior Changes

Android Oreo includes a variety of system and API behavior changes that can have an impact on the functionality of existing apps. These changes are described as follows.

Background Execution Limits

To improve the user experience, Android Oreo imposes limitations on what apps can do while running in the background. For example, if the user is watching a video or playing a game, an app running in the background could impair the performance of a video-intensive app running in the foreground. As a result, Android Oreo places the following restrictions on apps that are not directly interacting with the user:

  1. Background Service Limitations – When an app is running in the background, it has a window of several minutes in which it is still allowed to create and use services. At the end of that window, Android stops the app's background service and treats it as being idle.

  2. Broadcast Limitations – Android 7.0 (API 25) placed limitations on broadcasts that an app registers to receive. Android Oreo makes these limitations more stringent. For example, Android Oreo apps can no longer register broadcast receivers for implicit broadcasts in their manifests.

For more information about the new background execution limits, see the Android Developer Background Execution Limits topic.

Breaking Changes

Apps that target Android Oreo or higher must modify their apps to support the following changes, where applicable:

  • Android Oreo deprecates the ability to set the priority of individual notifications. Instead, you set a recommended importance level when creating a notification channel. The importance level you assign to a notification channel applies to all notification messages that you post to it.

  • For apps targeting Android Oreo, PendingIntent.GetService() does not work due to new limits placed on services started in the background. If you are targeting Android Oreo, you should use PendingIntent.GetBroadcast instead.

Sample Code

Several Xamarin.Android samples are available to show you how to take advantage of Android Oreo features:

  • NotificationsChannels demonstrates how to use the new Notification Channels system introduced in Android Oreo. This sample manages two notifications channels: one with default importance and the other with high importance.

  • PictureInPicture demonstrates basic usage of the Picture-in-Picture (PiP) mode for handheld devices introduced in Oreo. The sample plays a video which continues uninterrupted while switching back and forth between display modes or other activities.

  • Downloadable Fonts provides an example of how to use the Downloadable Fonts feature described earlier.

  • Location Updates Pending Intent illustrates usage of the Location API to get updates about a device's location using a PendingIntent.

  • Location Updates Foreground Service demonstrates how to use the Location API to get updates about a device's location using a bound and started foreground service.

Summary

This article introduced Android Oreo and explained how to install and configure the latest tools and packages for Xamarin.Android development on Android Oreo. It provided an overview of the key features available in Android Oreo, with links to example source code for several new features. It included links to API documentation and Android Developer topics to help you get started in creating apps for Android Oreo. It also highlighted the most important Android Oreo behavior changes that could impact existing apps.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.