I tried to change the language of the application with the menu resources as follows:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.eng: String languageToLoad = "en"; Locale locale = new Locale(languageToLoad); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; // deprecated!! getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); // deprecated!! this.setContentView(R.layout.activity_main); break; case R.id.de: languageToLoad = "de"; locale = new Locale(languageToLoad); Locale.setDefault(locale); config = new Configuration(); config.locale = locale; // deprecated!! getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); // deprecated!! this.setContentView(R.layout.activity_main); break; } return super.onOptionsItemSelected(item); }
Also compileSdkVersion 25 , minSdkVersion 16 , targetSdkVersion 25 I know that the replacement is to use createConfigurationContext , but I don't know how to use it. I tried this, but this did not work:
switch (item.getItemId()) { case R.id.eng: String languageToLoad = "en"; Locale locale = new Locale(languageToLoad); Locale.setDefault(locale); Configuration overrideConfiguration = getBaseContext().getResources().getConfiguration(); overrideConfiguration.setLocale(locale); Context context = createConfigurationContext(overrideConfiguration); Resources resources = context.getResources(); this.setContentView(R.layout.activity_main); break;
How do I apply this new method to my code ??
source share