0

I am making a multilanguage android project. I did it, but when I closed the app, it was back to default language. I was googling and searched some topics on stackoverflow. They say, I have to save the current language with SharedPreferences. I tried it, but it didn't work. PLz check What I am doing wrong. My code:

public class setting extends Fragment {

/**
 * @param args
 */
private Spinner spinnerctrl;
private Locale myLocale;
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View settingView =  inflater.inflate(R.layout.setting, container, false);
    spinnerctrl = (Spinner) settingView.findViewById(R.id.spinner1);
    spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (arg2 == 1) {
                Toast.makeText(arg0.getContext(), "You have selected English", Toast.LENGTH_SHORT).show();
                setLocale("en");
            } else if (arg2 == 2) {
                Toast.makeText(arg0.getContext(), "You have selected VietNam", Toast.LENGTH_SHORT).show();
                setLocale("vi");
            }
        }

        private void setLocale(String lang) {
            // TODO Auto-generated method stub
            SharedPreferences prefs = getActivity().getSharedPreferences(
                      "com.example.app", Context.MODE_PRIVATE);
            myLocale = new Locale(lang);
            Resources res = getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            Configuration conf = res.getConfiguration();
            conf.locale = myLocale;
            res.updateConfiguration(conf, dm);
            Intent refresh = new Intent(getActivity(), MainActivity.class);
            startActivity(refresh);
            String lan = prefs.getString("language", Locale.getDefault().getLanguage() );
            setLocale(lan);
            prefs.edit().putString("language", "en").apply();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }

    });
    return settingView;
}
vog
  • 23,517
  • 11
  • 59
  • 75

1 Answers1

0

In your setLocale(), first you should store preference, and then retrieve it. You are doing it in reverse way. So,

prefs.edit().putString("language", "en").apply();

should come first, and then

String lan = prefs.getString("language", Locale.getDefault().getLanguage() );

Updated function:

private void setLocale(String lang) {
    /*SharedPreferences prefs = getActivity().getSharedPreferences(
              "com.example.app", Context.MODE_PRIVATE);*/

    SharedPreferences setPrefs = getApplicationContext().getSharedPreferences("com.example.app", MODE_PRIVATE);
    SharedPreferences.Editor editor = setPrefs.edit();
    editor.putString("language", "en");
    editor.commit();

    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(getActivity(), MainActivity.class);
    startActivity(refresh);

    /*prefs.edit().putString("language", "en").apply();
    String lan = prefs.getString("language", Locale.getDefault().getLanguage() );
    //setLocale(lan);   //Remove this. It will create infinite loop */

    SharedPreferences getPrefs = getApplicationContext().getSharedPreferences("com.example.app", MODE_PRIVATE);
    String lan = getPrefs.getString("language", "");
    System.out.println("-- locale from getPrefs " + lan);

}

To know more about SharedPreferences check this link.

Community
  • 1
  • 1
Paritosh
  • 2,097
  • 3
  • 30
  • 42
  • It means I have to put prefs.edit().putString("language", "en").apply(); first then String lan = prefs.getString("language", Locale.getDefault().getLanguage() ); Still not work @@ sorry i'm not good at SharedPreferences – user3513645 Apr 18 '15 at 07:25
  • Replace `setLocale()` and reply. – Paritosh Apr 18 '15 at 07:35
  • remove setLocale(lan)? – user3513645 Apr 18 '15 at 07:48
  • I think you should check shared pref for locale, and if not set, ask user to set it. Add log or sout after `prefs.getString()` to check if it is set correctly. Remove `setLocale(lan);` – Paritosh Apr 18 '15 at 07:55
  • I updated `shared prefs` and tested it. Now its working. Also I commented shared prefs code we used earlier so that you can compare it. – Paritosh Apr 18 '15 at 12:01
  • I copied your code. But it still back to default language...Awwwww I'm going crazy – user3513645 Apr 18 '15 at 12:55
  • :) You are right. I have given this code to demonstrate working of shared prefs. Check `system.out` line. As I suggested, you will set prefs in `setLocale(String lang)` and get prefs once app is launched. I am not having that code. So you have to paste 3 lines of get prefs and check if `lan` string is empty i.e. "" – Paritosh Apr 18 '15 at 13:14