While developing your awesome application, sometimes you are required to add a feature to change the language of your app on the fly. However, Android OS does not directly support this behaviour. And therefore, you need to solve this situation in some other ways.
Android by default uses the locale of the device to select the appropriate language dependent resources. And most of the time this behaviour is enough for common applications.
However, sometimes there is some business requirements that you need to implement. To do that I will outline the details of changing the language of your application programmatically on the fly.
Here is the appropriate way of changing the locale of the application:
There are some difficulties which you have to overcome to change the language programmatically.
- Your application will not remember your language change after it is closed or recreated during the configuration change.
- You should update currently visible UI properly according to the selected language.
“LocaleHelper” is the solution all you need. You just have to initialize locale on your application’s main class. After that all your language changes will persist.
After the recent changes in Android API Version 24(Nougat) we need to override attachBaseContext to reflect changes.
if you call onAttach(Context context) constructor it will just set default locale of your device as the default locale of your application.
if you call onAttach(Context context, String defaultLanguage) constructor it will set the given language as the default language of your application for the first time that your application opens. After that you can change your locale by using the buttons or any other method that you provide to your users through your layout.
You need to override attachBaseContext and call LocaleHelper.onAttach() to initialize the locale settings in your application.
Here you can find an example activity which has two buttons and three textviews on it to change the locale of the textviews on the fly.
In your activity override attachBaseContext and call LocaleHelper.onAttach(). After that you just need to take care of the activity or fragment which includes the language change buttons. Other activities will not require any modifications at all.
You have two options to update currently visible layout:
- First, you can just update the text or any other language dependent resources one by one.
- Second, you can call activity.recreate() to restart currently loaded activity. Then the activity will reload the resources with the correct locale. However, if you select this option your users will notice the affect because it will close your application and you get black screen for a very small amount of time and then recreate your activity again in meanwhile.
For your reference, I added a sample project on github which can be found here. The sample project is targeting Nougat API 25(7.1). You can easily change the target for your needs.
If you have any question regarding to change locale of your android application, feel free to ask.