Change app language programmatically in Android

前端 未结 30 2999
面向向阳花
面向向阳花 2020-11-21 04:34

Is it possible to change the language of an app programmatically while still using Android resources?

If not, is it possible to request a resource in an specific lan

30条回答
  •  半阙折子戏
    2020-11-21 05:13

    There are some steps that you should implement

    First, you need to change the locale of your configuration

    Resources resources = context.getResources();
    
    Configuration configuration = resources.getConfiguration();
    configuration.locale = new Locale(language);
    
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    

    Second, if you want your changes to apply directly to the layout that is visible, you either can update the views directly or you can just call activity.recreate() to restart the current activity.

    And also you have to persist your changes because after user closes your application then you would lose the language change.

    I explained more detailed solution on my blog post Change Language Programmatically in Android

    Basically, you just call LocaleHelper.onCreate() on your application class and if you want to change locale on the fly you can call LocaleHelper.setLocale()

提交回复
热议问题