Exploring Android 13: Per-App Language Preferences

Android 13 (Tiramisu)’s new APIs and features are ready to be tried out by developers. One of the new features offered in this phase is Per-App Language Preferences (or In-App Language Preferences).

By default, an Android application will inherit the device’s language. This might be enough for most cases, but what if the user want to use a different language for a certain app? With Android 13, you can build such in-app language preferences with ease.

In this article we will build a simple application that implements this new exciting feature. Here’s what it looks like when it’s done:

With the new Android 13’s per-app language preferences API, you can change the language within your app! (or return to using the device language.)

Where’s the Source Code?

Step by Step Explanation

Follow the steps provided below to make this application.

1. As of the writing of this article, Android 13 can only be used with the preview release of Android Studio. Therefore, you must go to this page, and download the Canary Build of Android Studio.

2. Once installed, you need to install the Android 13 SDK. You can do this by clicking Tools > SDK Manager. In the SDK Platforms tab, select Android 13. In the SDK Tools tab, select Android SDK Build-Tools 33. Click OK to install the SDK.

3. Once SDK is installed, open the Android Studio. Create new project with Empty Activity option.

4. First off, you need to change the target and compile SDK version of this app. Open app/build.gradle and you would see something like this:

Change the compileSdk and targetSdk to use the Android 13 SDK like shown in the code snippet below. Don’t forget to click Sync Now after.

5. First, we will prepare our strings. Because we’re going to switch between different languages, we have to prepare at least 2 languages. Here, I will use the default English (en) and Indonesian (id-ID/in-rID) languages. However, you’re free to use whatever languages you want. My values/strings.xml and values-in-rID/strings.xml will look like this:

6. Next, we will prepare our layout. We just need the default activity_main.xml for this. This is what the layout will look like in the end:

This is what activity_main.xml should look like.

Here’s the contain of activity_main.xml:

7. Finally, we’re going to need to update the content of MainActivity.kt. You just need to copy-paste the code snippet below. The most important thing to notice from this is the usage of LocaleManager.applicationLocales to get and set in-app language locale. Notice that if you’re using Java instead of Kotlin, you would use LocaleManager.getApplicationLocales and LocaleManager.setApplicationLocales instead.

8. That’s it! If you run it, your app should be able to switch between languages (and persist those changes!) without having to change the device’s language:

The end result of our application.

As always, thanks for reading!

Want to learn more about Android 13?

Check out all of our articles from “Exploring Android 13” series here:

Exploring Android 13 (Tiramisu)

10 stories

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store