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.
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.
The following is required to use Android Oreo features in Xamarin-based apps:
Visual Studio – If you are using Windows, version 15.5 or later of Visual Studio is required. If you are using a Mac, Visual Studio for Mac version 7.2.0 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.
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:
Update to the latest version of Visual Studio.
Install the Android 8.0.0 (API 26) or later packages and tools via the SDK Manager.
Create a new Xamarin.Android project that targets Android Oreo (API 26).
Configure an emulator or device for testing Android Oreo apps.
Each of these steps is explained in the following sections:
Update Visual Studio and Xamarin.Android
To add Android Oreo support to Visual Studio, do the following:
If you are using Visual Studio 2017:
Update to Visual Studio 2017 version 15.5 or later (see Update Visual Studio 2017).
Use the SDK Manager installation instructions to install the Xamarin SDK Manager. The Xamarin SDK Manager must be installed because Google does not provide a standlone GUI SDK manager that supports API 26.0 and later.
If you are using Visual Studio 2015, we we recommend downgrading SDK Tools to 25 and using the old Google Emulator manager GUI. SDK tools 25 can still be used alongside API 26, 27, and newer, and won't impact development for new platforms. This will give you an interface for managing your Android SDK for older versions of VS.
For more information about Xamarin support for Android Oreo, see the Xamarin.Android 8.0 release notes.
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.
Start the SDK Manager (in Visual Studio, click Tools > Android > Android SDK Manager).
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:
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):
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:
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 either the Xamarin Android Emulator Manager or the
avdmanager tool to create virtual devices for Android
To use the Xamarin Android Device Manager to create and manage virtual devices, see Xamarin Android Device Manager. To create virtual devices without the Xamarin Android Emulator Manager, follow the steps in the next section.
Creating Virtual Devices Using avdmanager
To use avdmanager to create a new virtual device, follow these steps:
Open a Command Prompt window and set
JAVA_HOMEto 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"
Add the location of the Android SDK
binfolder 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"
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"
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 avdmanager to create your virtual device, it will be included in the device pull-down menu:
For more information about configuring an Android emulator for testing and debugging, see Google Android 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.
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 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.
Notification Channels explains how to create a notification channel and use it for posting local notifications. For a real-world code example, see the NotificationChannels sample; this sample app manages two channels and sets additional notification options.
Notification badges are small dots that appear over app icons as shown in this screenshot:
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:
Create a Resources/font folder.
Copy your font files (example, .ttf and .otf files) to Resources/font.
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.ttfcould be renamed to something like
Apply the custom font by using the new
android:fontFamilyattribute in your layout XML. For example, the following
TextViewdeclaration 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.
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
(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:
Instantiate a FontRequest object.
Subclass and instantiate FontsContract.FontRequestCallback.
Implement the FontRequestCallback.OnTypeFaceRetrieved method, which is used to handle completion of the font request.
Implement the FontRequestCallback.OnTypeFaceRequestFailed method, which is used to inform your app of any errors that take place during the font request process.
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
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.
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).
The AutofillFramework sample demonstrates the use of the Autofill Framework. It includes implementations of client Activities with views that should be autofilled, and a Service that can provide autofill data to client Activities.
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:
To specify how your activity should behave when it is in PIP mode, you
use the new
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).
The PictureInPicture sample 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.
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.
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:
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.
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.
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.
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.
AutofillFramework demonstrates the use of the Autofill Framework. It includes implementations of client Activities with views that should be autofilled, and a Service that can provide autofill data to client Activities.
Downloadable Fonts provides an example of how to use the Downloadable Fonts feature described earlier.
EmojiCompat demonstrates usage of EmojiCompat support library. You can use this library to prevent your app from showing missing emoji characters as "tofu" characters.
Location Updates Pending Intent illustrates usage of the Location API to get updates about a device's location using a
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.
Android 8.0 Oreo development with C#
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.