Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to change the application language at runtime

Tags:

I want to let the user change the language of my application using spinner (or any way). I tried many ways but they change the language of this activity not all activities, and I want to save it so when the user restart the app he will find the last choosed language.

like image 942
Firas Al Mannaa Avatar asked Sep 01 '12 19:09

Firas Al Mannaa


People also ask

How do I change localization in Android?

You'll find this screen either in the System Settings app: Languages, or System Settings: System: Languages and input. The Language preference screen should contain one entry called “English (Europe)”. Click Add language and add a fallback language.


1 Answers

you can use this code in spinner or any way you want

String languageToLoad  = "en"; // your language  Locale locale = new Locale(languageToLoad);   Locale.setDefault(locale);  Configuration config = new Configuration();  config.locale = locale;  getBaseContext().getResources().updateConfiguration(config,   getBaseContext().getResources().getDisplayMetrics()); 

then you should save the language like this

SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE); SharedPreferences.Editor editor = languagepref.edit(); editor.putString("languageToLoad",languageToLoad ); editor.commit();   

and use the same code in every activity in onCreate() to load the languageToLoad from the SharedPreferences

like image 135
Abol3z Avatar answered Oct 20 '22 10:10

Abol3z