1

I'm trying to change the app language in Java-Based Android. This is the function:

public void setLocale(String lang) {
    Locale locale = new Locale(lang);
    Configuration config = new Configuration();
    
    Locale.setDefault(locale);
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, null);
    finish();
    startActivity(getIntent());
}

This function is from another question like "how to change the application language", BUT IT WORKS ONLY ONCE (for example from English to Hebrew). this is not a dublicate question
Can you please help me?

L-S
  • 189
  • 1
  • 9
  • 3
    Does this answer your question? [Change app language programmatically in Android](https://stackoverflow.com/questions/2900023/change-app-language-programmatically-in-android) – Leonardo Velozo Feb 13 '21 at 02:21
  • Change the language and set languageValues to true which is of boolean type.Now every time check languageValue when activity lunches. – Rahul Kushwaha Feb 13 '21 at 05:42
  • 1
    @LeoPelozo this is not a duplicate, because the way I did it (like the answer of the question that you linked) worked but not on 100% of the application – L-S Feb 13 '21 at 15:49

1 Answers1

6

Replace your function with this one:

public void setLocale(String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale); // changes the languages
    Resources resources = getBaseContext().getResources();
    Configuration config = resources.getConfiguration();
    config.setLocale(locale);
    resources.updateConfiguration(config, resources.getDisplayMetrics());
    this.recreate(); // re-launches the app
}
hata
  • 11,633
  • 6
  • 46
  • 69