Reputation: 803
I have the following code:
locale = new Locale(loc);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
Resources resources = getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
Intent intent = new Intent(getActivity(), Settings.class);
getActivity().finish();
startActivity(intent);
In my Preference settings screen, I obtain the value of ListPreference and onChange of the value the above code snippet is called.
However, the call happens, but the strings don't change to "ta" language. This is done inside a PreferenceFragment which is called by Settings Activity.
Text within the app changes the language to appropriate one, but the text on the toolbar doesn't change.
Another thing to note is that - Changing the locale in Settings activity reflects only in that activity and not in the whole of the app. How is this achievable?
Can someone guide?
Upvotes: 0
Views: 302
Reputation: 7918
When anything text-related is created in Android, it checks the locale when its being created and uses that locale to find the correct text. If you then change the locale at runtime, the text has already been created (with the old locale) and will therefore NOT change to the new locale. But if you open a new activity/fragment after changing locale, it will use the new locale.
I also ran into this problem when i implemented language settings in my application, and to fix it i ended up changing the locale, saving the new locale in SharedPreferences,then restarting the entire application, and in the OnCreate of your first activity, i use the locale i saved in SharedPreferences to change the locale of my app. (this has to be done before you set any kind of text in your app).
Upvotes: 1