How to change Android O / Oreo / api 26 app language

前端 未结 8 1496
深忆病人
深忆病人 2020-12-01 04:37

I want to change the language of the app and this works fine until API 26.

For api > 25 I put Locale.setDefault(Locale.Category.DISPLAY, mynewlanglocale);

8条回答
  •  情书的邮戳
    2020-12-01 05:20

    It is possible, however i would not recommend to set the language programatically

    Android is designed so the System UI and your App have the same language, if you change it programmatically you would be fighting the system

    Instead what you can do is enable multilanguage support by adding different strings.xml languages, this will change the language automatically

    I reccommend reading through this Google Developers article:

    Supporting Different Languages and Cultures

    If you really need to change it programatically you can do the following

    Locale locale = new Locale("en");
    Locale.setDefault(locale);
    
    Configuration config = context.getResources().getConfiguration();
    config.setLocale(locale);
    context.createConfigurationContext(config);
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    

    On SDK >= 21, you need to call 'Resources.updateConfiguration()', otherwise resources will not be updated.

    Hope it helps.

提交回复
热议问题