Change the Locale at Runtime

Change the locale at runtime?

Since API 11 you can use recreate so can make this method in your activity:

private void restartInLocale(Locale locale)
Configuration config = new Configuration();
config.locale = locale;
Resources resources = getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());

Changing locale at runtime in Swing

  1. You have a method that is used to change app locale (and probably persist the new value) and another one to get localized strings.

  2. Create an interface:

    interface LocaleChangeListener {
    void onLocaleChange();

    Implement it by UI components that need to be able to change locale at runtime and set the new values in overrides onLocaleChange().

  3. Now, have a list of listeners that will be notified on locale change by the first method.

Android locale not (completely) changing at runtime

I just copy-pasted this from my old source, based on Java.

Override this in your Application class and Mainactvity.

protected void attachBaseContext(Context base) {
Log.e(TAG, "attachBaseContext: ");

How to use: Whenever you click something, invoke like this.


language is hi (Hindi) and the region is India (IN)


LocaleHelper.setLocale(this, "hi", "IN");

recreate(); //now restart.

A Local Helper class

 public class LocaleHelper {
private static final String TAG = "DAFT_PUNK_LH : ";

private static final String SELECTED_LANGUAGE = "en";
private static final String SELECTED_LANGUAGE_COUNTRY = "US";

public static Context onAttach(Context context) {
Log.d(TAG, "onAttach:");
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
String langCountry = getPersistedCountryData(context, Locale.getDefault().getCountry());
return setLocale(context, lang, langCountry);

public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());

public static String getLanguageCountry(Context context) {
return getPersistedCountryData(context, Locale.getDefault().getCountry());

public static Context setLocale(Context context, String language, String langCountry) {
Log.d(TAG, "setLocale: ");
persist(context, language, langCountry);

return updateResources(context, language, langCountry);


private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);

private static String getPersistedCountryData(Context context, String defaultLangCountry) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE_COUNTRY, defaultLangCountry);

private static void persist(Context context, String language, String langCountry) {
Log.d(TAG, "persist: ");
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();

editor.putString(SELECTED_LANGUAGE, language);
editor.putString(SELECTED_LANGUAGE_COUNTRY, langCountry);

private static Context updateResources(Context context, String language, String langCountry) {
Log.d(TAG, "updateResources: ");
Locale locale = new Locale(language, langCountry);

Configuration configuration = context.getResources().getConfiguration();

return context.createConfigurationContext(configuration);

Android how to change the application language at runtime

you can use this code in spinner or any way you want

String languageToLoad  = "en"; // your language 
Locale locale = new Locale(languageToLoad);
Configuration config = new Configuration();
config.locale = locale;

then you should save the language like this

SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",languageToLoad );

and use the same code in every activity in onCreate() to load the languageToLoad from the SharedPreferences

Dynamically change the locale in Angular 7 at runtime without having to reload the app

I think you don't need to reload, but just delay a little to ending registerCulture

for example:

english() {; // <-- start any loader
setTimeout(() => {
spinner.hide(); // <-- stop the loader
}, 1000);
// window.location.reload();

Change language at runtime in android

1.try like this it work for me

public static void changeLanguage(String languageCode, Context context)
Locale locale = new Locale(languageCode);
Configuration config = new Configuration();
config.locale = locale;
.updateConfiguration(config, context.getResources()


Change display language at runtime and recreate all activities

Add "locale" property in your android:configChanges in all you activities and then override onConfigurationChanged() in activities and handle the language changes accordingly.

More on

Changing Locale at runtime not effecting fragments

I assume that you are using this approach. Where its not suggested any change in the AndroidManifest.xml so the android:configChanges="locale" may cause the misbehavior you defined.

For the date formating you should take into consideration that your application is not using the Locale.getDefault() but something different that is defined by the user and your LocaleHelper mechanism.

Some extra details about config change

The android:configChanges means you don't want the system to recreate your activity when one of the provided attribute happens. In your case the locale.

In terms of development with that approach in order to properly handle this option in the manifest you have to implement the

public void onConfigurationChanged(Configuration newConfig) {
// refresh your views here

and then perform your own handling. Something that you didn't required in your case.

Related Topics

Leave a reply