change language programmatically in android

Update:

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

While developing your awesome awarding, sometimes you are required to add a feature to change the language of your app on the fly. Nonetheless, Android Os does non directly back up this behaviour. And therefore, you need to solve this state of affairs in some other ways.

Android by default uses the locale of the device to select the appropriate language dependent resources. And about of the fourth dimension this behaviour is enough for mutual applications.

Notwithstanding, sometimes at that place is some business requirements that y'all need to implement. To do that I will outline the details of irresolute the language of your awarding programmatically on the fly.

Here is the advisable style of changing the locale of the awarding:

In that location are some difficulties which you have to overcome to change the linguistic communication programmatically.

  1. Your application volition not remember your linguistic communication modify afterwards it is closed or recreated during the configuration change.
  2. You should update currently visible UI properly according to the selected linguistic communication.

Solution

"LocaleHelper" is the solution all y'all need. You only have to initialize locale on your application's main course. After that all your language changes will persist.

After the recent changes in Android API Version 24(Nougat) nosotros need to override attachBaseContext to reflect changes.

LocaleHelper.javA

if you lot telephone call onAttach(Context context) constructor information technology will just set default locale of your device as the default locale of your application.

if you phone call onAttach(Context context, Cord defaultLanguage) constructor information technology will gear up the given linguistic communication equally the default linguistic communication of your application for the first time that your application opens. Later on that you tin change your locale by using the buttons or whatever 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 instance activity which has two buttons and three textviews on it to change the locale of the textviews on the fly.

In your action override attachBaseContext and call LocaleHelper.onAttach(). Afterwards that you but need to take intendance of the activity or fragment which includes the language modify buttons. Other activities volition non require whatever modifications at all.

Y'all have two options to update currently visible layout:

  • Start, you tin just update the text or any other language dependent resources i past one.
  • Second, you can phone call activity.recreate() to restart currently loaded activeness. Then the activity volition reload the resource with the correct locale. Nevertheless, if you lot select this option your users will notice the touch because it will close your application and you get blackness screen for a very small corporeality of time and then recreate your activity again in meanwhile.

Sample Project

For your reference, I added a sample project on github which tin be found here. The sample project is targeting Nougat API 25(7.1). You can hands change the target for your needs.

If you take any question regarding to modify locale of your android application, feel free to inquire.

Update for new App Bundle Format

Google introduced a new App Bundle format to split up apk files in smaller sizes when they're being installed on the customer devices. However, this ways that nosotros cannot have dynamic linguistic communication changes in our applications.

To foreclose that split up we demand to add extra lines in our build.gradle file inside the app folder like below.