Xavier Rubio Jansana

Xavier Rubio Jansana

Mobile engineer.
Android and iOS.
Scuba diver.

© 2024

Language change detection in Android Jellybean 4.2+

In the current project I’m working on we had to detect system language change. We wanted to rely on Android configuration change mechanism, that handles this for us, but we had to do some additional state modifications beside UI.

So, the easy way is to detect configuration changes and handle them, following Handling Runtime Changes. Ok, so we decided to add a android:configChanges attribute with a value "locale" to the activity and add override onConfigurationChanged in the activity… and surprisingly nothing happened.

After some investigation we found that the problem happens on API 17+ and that the reason is that for this API, both "locale" and "layoutDirection" has to be specified, as Android now generates both in response to a language change (similarly to what happened in API 13+ with "orientation" and "screenSize"). The original answer came from StackOverflow.

BTW at the end we didn’t use this solution. Instead we used a BroadcastReceiver for the intent ACTION_LOCALE_CHANGED. The reason is this way we allow Android normal mechanism to handle the Activity restart for us (handling the configuration change by ourselves sidesteps this) and at the same time detect the language change.