-3

I have created 4 languages for the app. I can change the Lauaguage, ok, but if close the app and then start I it again, the app starts atfirst with the default string.xml.

How to let the app starts with the last selected Language ?

should I call the the methode by OnCreate in the mainActivity ?

     @SuppressWarnings("deprecation")
    public void setLocale(String lang) {
        Locale myLocale = new Locale(lang);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        Configuration conf = getResources().getConfiguration();
        conf.locale = myLocale;
        getResources().updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, Languages.class);
        startActivity(refresh);
        /*         "en" = English
            "hi" =Hindi
            "fr" =French
            "it" =Italian
            "de" =German
            "es" =Spanish
            "ja" =Japanese
            "ko" =Korean
            "nl" =Dutch
            "pt" =Portuguese
            "ru" =Russian
            "zh" =Chinese
            "ar" = arabic
   */
    }

How can the user change the default language ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Biblio
  • 43
  • 1
  • 6

2 Answers2

1

Why don't you store the selected language in shared preferences? That way you can always check for the selected language when your app starts, and then load the appropriate language file.

Mohale
  • 2,040
  • 3
  • 17
  • 18
  • Yes I did that, I save the selected language symbol like En, De, Fr, ... and the image-path of the flag as string in shared preference too. Bave ut the problem, i have to select the language every time after restart of thre app. I 've found here something simulare but did not work : [link](https://stackoverflow.com/questions/12908289/how-to-change-language-of-app-when-user-selects-language) – Biblio Jul 06 '17 at 07:50
  • You might have to use take the advantage of activity life cycle functions like `onCreate` , `onResume` and others to load the file. At which point do you check the selected language? – Mohale Jul 06 '17 at 07:55
  • @Biblio `... and the image-path of the flag as string in shared preference ...` **WRONG**. You can have localized `drawable` folders too. No need to do naive things like storing the path to the correct flag image. – Phantômaxx Jul 06 '17 at 08:00
  • I use extra Activity for changing the Language : Laguage.java. the Main is MainActivity. if I change the language in the Activity Language, I go back automaticlly to MainActivity again, the language and the flag(img) will be changed. everything OK. The Problem is : If close the App and start it again, the selected Language will be lost, I get the default one again. – Biblio Jul 06 '17 at 08:05
  • @Biblio As suggested, use the SharedPreferences to store the language upon change. Update the locale in your app. On start, check the setting in your SharedPreferences and set the locale accordingly. DONE. – Phantômaxx Jul 06 '17 at 08:11
  • @Rotwang : I use an Image as Button to change the Language, so I load the imgage from Assets folder according to selected language. – Biblio Jul 06 '17 at 08:14
  • @Biblio **WRONG** You should load it from the proper `drawable` folder. It's all well explained in the official docs. You are overcomplicating your app. – Phantômaxx Jul 06 '17 at 08:15
0

I have used a long Way like below but it works. Thanks :

OnResume :

    selected_lang= myshared.getString("selected_lang","de"); 
    lang_found= Integer.parseInt(myshared.getString("lang_found","0"));  
setLocale(selected_lang);



@SuppressWarnings("deprecation")
public void setLocale(String lang) {
    Locale myLocale = new Locale(lang);    
    DisplayMetrics dm = getResources().getDisplayMetrics();
    Configuration conf = getResources().getConfiguration();
    conf.locale = myLocale;
    getResources().updateConfiguration(conf, dm);

    if(lang_found==0) {    
        Intent refresh = new Intent(this, MainActivity.class);
        startActivity(refresh);
        lang_found=1;    
    }

@Override
protected void onDestroy() {

    lang_found=0;
    Save_setting();
    super.onDestroy();
}
Biblio
  • 43
  • 1
  • 6