0

I want to change my app language in my app because android languages don't have all languages i want to use. So i got this to change:

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

However, when user opens app and change it. Then strings which is showing at that moment still are the older language it changes just when new Activity is created.

Other problem that i should somehow save what user language was select and then change language when app is started.

So how to improve this? I want that when user select language all strings would be taken from selection languages strings.xml and how to save which language user was selected?

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Streetboy
  • 4,351
  • 12
  • 56
  • 101

2 Answers2

0

You can use a SharedPreference to store the user's language. Here's the documentation about it: http://developer.android.com/reference/android/content/SharedPreferences.html

About the need to reload the Activity, you may find some answers here: Android: locale(system Language) change effect my application layouts

Community
  • 1
  • 1
Jeje Doudou
  • 1,707
  • 1
  • 16
  • 24
0

You can create a public class (public class Share) with static members like public static String language = "en"; You can easily access the fields of this class with using the code as Share.language in whole of your project. If you want to change the language you can set Share.language = "fa";. To reload the activity with selected language, you should place the following code before any activitie's super.onCreate(saveInstanceState); :

Locale l = new Locale(Shares.language);
Locale.setDefault(l);
Configuration config = new Configuration();
config.locale = l;
context.getApplicationContext().getResources()
        .updateConfiguration(config, null);

Please note that, language can be set, if you have a folder like values-language in your /res folder.

Good luck, Hossein :)

Hossein Mobasher
  • 4,382
  • 5
  • 46
  • 73