3

I have multilanguage android app. I need to set different language than user has in device settings. When my app is started, user choose from prefered language. On android 7 and below I use updateconfuguration in my application class. But this solution not work on android 8. I try to use update configuration in attachBaseContext in every activity, but without success.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • check out this post: link--> https://stackoverflow.com/questions/45088039/set-different-language-in-android-studio/45088145#45088145 – yash786 Sep 04 '17 at 10:02
  • 2
    Welcome to stackoverflow. Please, could you provide some code of your specific problem. That prove how far did you try and it will help other members to understand your problem better, at the time, you will give them a context of your issue. Please, check these links: https://stackoverflow.com/help/mcve and https://stackoverflow.com/help/how-to-ask – Kenzo_Gilead Sep 04 '17 at 10:06
  • 1
    I know exactly what he means because I am facing the same problem. The solution that works for Android 7 and below does not work for Android 8. Michael, have you found a solution? I'm currently investigating and will post and update when I figure it out. – AutonomousApps Sep 14 '17 at 03:15
  • Did you get solution for this? – diordna Sep 27 '17 at 06:52
  • No, I didn't find a solution yet. I try override attachBaseContext witn change locale in activities. This solution works, but it breake autofill framework, and I must call it in every activity – Michael Drdlíček Oct 02 '17 at 07:21
  • @MichaelDrdlíček just use it in a base activity class. What do you mean, though, that it breaks the autofill framework? – AutonomousApps Oct 29 '17 at 19:06
  • When I change locale in attachBaseContext, autofill suggest from android oreo not work on this activity. I don't know why. – Michael Drdlíček Oct 30 '17 at 09:43

2 Answers2

5

@codespy has the right idea, but to provide more detail:

I had the exact same issue in my app. I initially thought the problem was using this deprecated method of setting a custom language:

Locale.setDefault(newLocale);
Configuration config = new Configuration();
// TODO fix deprecation issues
config.locale = newLocale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());

And so I tried to use createConfigurationContext, with help from this answer, but that wasn't working either (and moreover was much more complex with several gotchas).

After extensive debugging, I noticed something:

MyAdapter adapter = new MyAdapter(getApplicationContext(), list);

On a hunch, I replaced getApplicationContext() with getContext(), and all of a sudden my list of items was using the proper language.

Turns out Oreo's getApplicationContext() no longer respects the custom locale you set. You must use the activity's context instead.

(Please note: I know I shouldn't have been using an app context for this, but this is a legacy app with many such issues. Fixing them is my ongoing daily struggle.)

AutonomousApps
  • 4,229
  • 4
  • 32
  • 42
  • Thanks a lot, you just saved my day :) – Wael Abo-Aishah Oct 29 '17 at 12:36
  • Had a similar issue when running instrumentation / ui tests. Solved it by using InstrumentationRegistry.getTargetContext() instead of InstrumentationRegistry.getContext(). Thanks for the hint. – Gandora Jul 02 '19 at 06:53
-1

Please use activity.getResource() and use this Resource object. In my case this help.

codespy
  • 19
  • 1
  • 3
  • 1
    This isn't a clear solution, consider adding some line of explanation, for example how to use the Resource object – Luca Murra Dec 29 '18 at 15:04