Exploring the new Android 13 Per-app Language preferences

Heitor Paceli
ProAndroidDev
Published in
3 min readMar 2, 2022

--

How to use the new feature from Android 13 to display your app in a language different of the system one

Photo by Wilhelm Gunkel on Unsplash

Recently Google released the developer preview of Android 13 — also known as Tiramisu — that introduces a new feature which developers have been waiting for a certain time: the Per-app language preferences.

This new feature make it possible to run the application under locales that are not necessarily the same set at Android settings, providing a better experience for multilingual users. It allows applications to change the locale preferences at runtime using the newly introduced APIs.

Changing application locales at Runtime

This article will explain how to implement an app that uses the new Per-app language preferences.

App project

Once this feature was introduced in the Android 13 developer preview, make sure to set the right versions in the SDK configurations at your build.gradle.

We set the minSdkPreview and targetSdkPreview to Tiramisu and compileSdkPreview to android-Tiramisu. You can see the complete build.gradle below:

Configuration of build.gradle to use Android Tiramisu developer preview SDK

Localized strings

Having the project created and configured, it’s time to add some localized text so we are able to see if the application language is really changing as expected.

Photo by Markus Krisetya on Unsplash

So let’s add a couple of locales to the application using the Android Studio’s Translations Editor as usual, and then adding a new string with our very famous sentence, Hello World, and its localized versions.

I am using Google Translate to create the localized strings, so please forgive me if anything is not translated as expected.

Android Studio’s Translations Editor

Locale Picker

Now that the application already have the localized text to be displayed to the user, we just need to provide a way that they can choose which language should be used while running our app.

In order to do that, let’s create an Spinner with the supported locale tags and also an option to reset language preference and use system locale. Then set the Spinner’s onItemSelectedListener in order to call the code that actually changes the application language:

Code to initialize the locale picker

Changing App’s Locale

In order to change current application’s locale we need to obtain an instance of LocaleManager using the getSystemService method from Context class. Then create a new LocaleList with the new locales that must be used by the application and pass it as argument to setApplicationLocales.

Note: LocaleList is a class available in Android SDK. It is not the same as a List of Locale instances.
Code to update the application locales preference

To reset application locales and use the system’s one, pass an empty LocaleList that can be obtained by calling LocaleList#getEmptyLocaleList().

Calling getApplicationLocales will return the list of locales that are currently set for the application. Let’s use it to update the activity’s title with the display name of the current locale:

Code to update activity’s title with the display name of current locale

Now the application is ready. You can find the complete activity’s code below:

Conclusion

You can read more about this feature at its Android developers page.

Please be aware that it is still under developer preview phase and it may be modified or removed at the final version. I’ll be updating the GitHub repository with any changes that may be required when the final version is released.

If you find this article helpful, please don’t forget to clap. I would also appreciate your feedback in the comments section. Thanks!

--

--