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

前端 未结 8 1472
深忆病人
深忆病人 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.

    0 讨论(0)
  • 2020-12-01 05:20

    After use all solution in all sources finally i found my issue. That makes me angry for 2 days.

    Everyone knows that in Android Oreo (API 26) we must use createConfigurationContext, But My problem is using Country name with local.

    Replace

    en_US with en

    ar_AE with ar

    fa_IR with fa

    And my problem solved.

    Hope to help someone

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