How to change the language at runtime

I read about localization here: http://developer.android.com/guide/topics/resources/localization.html But I need to switch the language in my android application at runtime, for example. through Spinner.

I tried to make subj this way

DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = new Locale(language_code.toLowerCase(),
coutry_code.toUpperCase());
res.updateConfiguration(conf, dm);

but all changes are applied only after restarting the application. Can anyone help me?

+3
source share
1 answer

In the localization guide, it automatically updates ... and when I use the spinners in my application, it updates correctly. Do you have a spinner class configured at the bottom of your class? or just in choosing your counter, restart it as:

//spinner class start...
if(selected.equals("english") //given selected is a string returned by Spinner
{
   //normal spinner content you have goes here, then
   //for example, finish method, then restart with an intent
   finish()
   Intent myIntent = new Intent(main.this,main.class);
   main.this.startActivity(myIntent);
}
 //or..
if(selected.equals("french"){ // continued.. 
   refresh();
}

//given that refresh();
public void refresh(){
   finish()
   Intent myIntent = new Intent(main.this,main.class);
   main.this.startActivity(myIntent);
}
0
source

Source: https://habr.com/ru/post/1771335/


All Articles