How can I change language of whole application by only single click?

后端 未结 1 1646
不思量自难忘°
不思量自难忘° 2020-12-19 14:03

I have a Spinner with three language choice on login page. I want when user choose one language suppose \"Persian\" language the language of wh

相关标签:
1条回答
  • 2020-12-19 14:11

    Change Language Programmatically in Android

    Here is the appropriate way of changing the locale of the application:

    There are some difficulties which you have to overcome to change the language programmatically.

    1.)Your application will not remember your language change after it is closed or recreated during the configuration change.

    2.)You should update currently visible UI properly according to the selected language.

    Solution

    LocaleHelper” is the solution all you need. You just have to initialize locale on your application’s main class. After that all your language changes will persist.

    After the recent changes in Android API Version 24(Nougat) we need to override attachBaseContext to reflect changes.

    Below method used to change language for application :

    private static boolean updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
    
        Resources resources = context.getResources();
    
        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
    
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    
        return true;
    }
    

    find more details in below link :

    Changing Android’s Locale Programmatically

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