0

Problem looks like that. In my app I have 3 activities (so far, fourth is comming). One of them is settings screen where user can choose his language. When settings are changed I update activities and now magic comes... all activities change their language except one - main menu activity. No matter what I do it works for all activities but this one. Funny thing is that I've checked and method to update locals is done and also proper language setting is used.

Main activity code (I've removed code that doesn't matter in this case):

public class MainMenuActivity extends AppCompatActivity {

private Preferences preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    initialize();

}

@Override
public void onResume() {
    applySettings();
    super.onResume();
}

private void initialize() {
    preferences = Preferences.getInstance();
    preferences.initiate(this);
}

private void applySettings() {
    Toast.makeText(this, getResources().getConfiguration().locale.getDisplayName(), Toast.LENGTH_LONG).show();
    Activity.setLocale(getApplicationContext(), preferences.getLanguage().getValue());
}

}

Here's method that sets locals:

public class ActivitySettings {

public static void setLocale(Context context, String lang) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration conf = new Configuration();
    conf.locale = locale;
    Resources res = context.getResources();
    res.updateConfiguration(conf, res.getDisplayMetrics());
}

As you see I've added line to show current language before update, and it show that language is correct. Why it won't change language for main activity?!


EDIT - found a solution

I've added new variable in my main activity

private Locale currentLocal = null;

Then on initiation I set this to current locale

currentLocal = Locale.getDefault();

Now in onResume() I've added that

if (currentLocal.getLanguage() != Locale.getDefault().getLanguage()) {
        currentLocal = Locale.getDefault();
        recreate();
    }

Now it works perfect.

Dzikus PL
  • 1
  • 2
  • 1
    Look at http://stackoverflow.com/questions/13181847/change-the-locale-at-runtime?answertab=active#tab-top – Huseyin Ozer Oct 17 '16 at 14:08
  • Although question you directed me to was not strictly about my problem, but it gave me a hint and now it works! thanks :) – Dzikus PL Oct 18 '16 at 09:41

1 Answers1

0

At first download this Helper class from here , now copy LocaleHelper.java into your project . After that create a class which will extent Application , now in the onCreate of your application put this LocaleHelper.onCreate(getApplicationContext())

Example

public class SampleApplication extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        initializeConfigurations(getApplicationContext());
        LocaleHelper.onCreate(getApplicationContext());
    }
}

Don't forget to mention it in manifest file

<application
    android:name=".SampleApplication"
    ...........................

Now you can set your Locale from anywhere in the app like this LocaleHelper.setLocale(context,"fr") , in this example I just set the Locale to french which will effect all activities .

Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64