Change Language Programmatically in Android

Gunhan Sancar
2 min readOct 8, 2015

--

Published locale-helper as a library which can be found at https://github.com/zeugma-solutions/locale-helper-android

While developing your awesome application, sometimes you are required to add a feature to change language on the fly. However, Android OS does not directly support this behavior. 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 behavior 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.

  1. Your application will not remember your language change after it is closed or recreated during the configuration change.
  2. You should update currently visible UI properly according to the selected language.

Solution

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.

p.s: After the recent changes in Android API Version 24(Nougat) we need to override attachBaseContext to reflect changes.

LocaleHelper.java

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.

MainApplication.java

You need to override attachBaseContext and call LocaleHelper.onAttach() to initialize the locale settings in your application.

MainActivity.java

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 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.

If you have any question regarding to change locale for your android application, feel free to ask.

Originally published at gunhansancar.com on October 8, 2015.

--

--

Gunhan Sancar

Software Engineer. Mobile Application Developer. IoT Enthusiast.