After the screen rotation, the language of my application will be changed

后端 未结 3 1533
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 17:58

I have created a bilingual (having two languages) android application. I have inserted my resource strings in two files:

For Persian language (default)
values/st         


        
3条回答
  •  臣服心动
    2021-02-13 18:29

    You can make class which extends Application. There you can override one method which gets called everytime you change configuration (example when screen orientation gets changed).

    Something like:

    public class MyApplication extends Application {
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            setLocale();
        }
    
        private void setLocale() {
            Locale locale = new Locale("fa_IR");
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                  getBaseContext().getResources().getDisplayMetrics());
        }
    }
    

    And dont forget to declare it in the manifest: example of Application class

    In AndroidManifest.xml:

提交回复
热议问题