Guy
Guy

Reputation: 6522

Reading locale settings from shared preferences doesn't work for the MainActivity

I am changing Locale settings inside my app with this method:

public void setLanguageSettings(String localeLang){
    Locale locale = new Locale(localeLang);
    saveString("lang", localeLang);
    Locale.setDefault(locale);
    Configuration configSlo = new Configuration();
    configSlo.locale = locale;
    getBaseContext().getResources().updateConfiguration(configSlo, getBaseContext().getResources().getDisplayMetrics());
}

As you can see, the localeLang String variable is then saved to SharedPreferences. This variable is in my case either "sl" or "en" (default is "en").

Then I'm calling this (same) method everytime the application starts (every time MainActivity is opened):

String language = getString("lang");
setLanguageSettings(language);

The Locale is changed successfully (I checked with log) but the problem is that the change can't be seen in the MainActivity. If I open any other activity, the language is correct, but the MainActivity remains "en" even tho it is supposed to be "sl". I'd have to restart MainActivity for it to be "sl".

Upvotes: 1

Views: 687

Answers (1)

Guy
Guy

Reputation: 6522

I solved this question in a weird way. I just called setContentView again in the change language method:

setContentView(R.Layout.MainLayout);

Upvotes: 1

Related Questions