Set-up the application Language in Android Preferences

后端 未结 2 1002
悲&欢浪女
悲&欢浪女 2020-12-17 02:06

I would like the application language to be set according to the user preferences but up till now it doesn\'t work how I would like it to.

I have set the default val

相关标签:
2条回答
  • 2020-12-17 02:23

    OK it may help someone. I have added the folowing to the main activity manifest:

    android:configChanges="locale"

    Then when the user choses the preferences I have put a confirm button and then this button brings you to main activity that is why the lnagages gets reset.

    I have a static class where I have this code to change the locale:

    public static void updateLanguage(Context context, String idioma) {
        if (!"".equals(idioma)) {
            if ("castella".equals(idioma)) {
                idioma = "es_ES";
            } else if ("catala".equals(idioma)) {
                idioma = "ca_ES";
            }
            Locale locale = new Locale(idioma);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            context.getResources().updateConfiguration(config, null);
        }
    }
    

    end at every activity I have like 20 of them I call this method before:

    setContentView(R.layout.list_event);

    With these methods when I rotate the screen the activities don't change the language here is a link to a blog that helped me: http://adrianvintu.com/blogengine/post/Force-Locale-on-Android.aspx

    0 讨论(0)
  • 2020-12-17 02:38

    I would think that you need to be setting the locale in the MainActivity onCreate method. The same way you are setting it when the onSharedPreferenceChanged method.

    0 讨论(0)
提交回复
热议问题