3

I am looking for the best solution to change language in app without restart application for Android 7. A lot of solutions contains a deprecated methods for ex.

Locale locale = new Locale("en_US"); 
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);

Any ideas?

  • 1
    Which method there is deprecated? Did you read the documentation for it to see what it suggests? – OneCricketeer Nov 06 '16 at 17:17
  • 1
    Hope this helps http://stackoverflow.com/a/39595646/5727285 – Hamza Nov 06 '16 at 17:20
  • updateConfiguration(..), in documentation recommended a Locate builder but for me it's not work maybe i do this in wrong way. Maybe somebody can put here an example how to use it – Natalia Majkowska Nov 06 '16 at 17:20
  • 2
    Possible duplicate of [Configuration \`locale\` variable is deprecated - Android](http://stackoverflow.com/questions/39589841/configuration-locale-variable-is-deprecated-android) – pathfinderelite Nov 06 '16 at 17:22
  • 1
    @AmeerHamza still there an updateConfiguration(..) is deprecated – Natalia Majkowska Nov 06 '16 at 17:22
  • the dupe link will handle the deprecated call with annotation so follow the duplicate link implementation – Pavneet_Singh Nov 06 '16 at 17:23
  • @PavneetSingh so you recommend to use a deprecated code? because i don't understand your answer – Natalia Majkowska Nov 06 '16 at 17:27
  • try this one for updateConfiguration http://stackoverflow.com/a/40291570/5727285 – Hamza Nov 06 '16 at 17:27
  • when a function is deprecated then solution is either you get a replacement or you can use the deprecated one in old platform and new one for newer platforms , the appropriate version of that function will be called according to the annotation check – Pavneet_Singh Nov 06 '16 at 17:33
  • Ok i know how annotation works but updateConfiguration is use for evry platfrom in examples and this function is deprecated – Natalia Majkowska Nov 07 '16 at 08:48
  • @PavneetSingh Well... We all know we need to get a replacement or using annotation to handle older and newer platform differently. What we ask is, in the new platform, what should we use to handle this task? – Firanto Dec 21 '16 at 18:41
  • @Kurotsuki for API 25 and above use [createConfigurationContext](https://developer.android.com/reference/android/content/Context.html#createConfigurationContext(android.content.res.Configuration)) – Pavneet_Singh Dec 21 '16 at 19:07
  • 1
    use createConfigurationContext:https://github.com/hosamazzam/AndroidChangeLanguage/tree/master/app/src/main/java/com/freeapps/hosamazzam/androidchangelanguage – Eraise Apr 25 '17 at 02:15

1 Answers1

-1

try this code:

 Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    locale = new Locale("en_US);
    conf.setLocale(locale);
    res.updateConfiguration(conf, dm);
Mohammad Rbabah
  • 456
  • 2
  • 10