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:
Where’s the Source Code?
android-13-samples/per-app-language-preferences at master · hanmajid/android-13-samples
Sample projects for Android 13 (Developer Preview 1) - android-13-samples/per-app-language-preferences at master ·…
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:
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 (
in-rID) languages. However, you’re free to use whatever languages you want. My
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:
Here’s the contain of
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
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:
As always, thanks for reading!
Want to learn more about Android 13?
Check out all of our articles from “Exploring Android 13” series here: