0

I need to save the locale of an app whenever user selects one from spinner. Right now i's able to change it, but it need to save the session if user exits the app.

heres my code:

  Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getActivity().getBaseContext().getResources().updateConfiguration(config,
            getActivity().getBaseContext().getResources().getDisplayMetrics());

 SharedPreferences shp = getActivity().getSharedPreferences(
            "android.exaple.com.PREFERENCES", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = shp.edit();
    editor.putString("USER_LANGUAGE", lang);
    editor.apply();
Alimov Shohrukh
  • 65
  • 1
  • 5
  • 17

1 Answers1

1

Okay, Let's Give and Example for you, Okay I will use Aleart Dialog in this case, you can try it with spinner.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.chooselanguage).setItems(R.array.language, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                switch (i) {
                    case 0:
                        SharedPreferences ensharedPreferences = getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE);
                        SharedPreferences.Editor eneditor = ensharedPreferences.edit();
                        eneditor.putString("language", "en");
                        eneditor.commit();
                        Toast.makeText(MainActivity.this, "English Selected", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        SharedPreferences npsharedPrefrences = getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE);
                        SharedPreferences.Editor npeditor = npsharedPrefrences.edit();
                        npeditor.putString("language", "ne");
                        npeditor.commit();
                        Toast.makeText(MainActivity.this, "Nepali Selected", Toast.LENGTH_SHORT).show();
                        }
            }
        }).setCancelable(false).create().show();

Now, the language will save as per choice from top code. Now call them using following code on onStart() callback of your activity.

SharedPreferences sharedPreferences = this.getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE);
    String pine = sharedPreferences.getString("language", DEFAULT);
    String languageToLoad = pine;
    Locale locale = new Locale(languageToLoad);//Set Selected Locale
    Locale.setDefault(locale);//set new locale as default
    Configuration config = new Configuration();//get Configuration
    config.locale = locale;//set config locale as selected locale
    this.getResources().updateConfiguration(config, this.getResources().getDisplayMetrics());

You may also get problem like, If the user selected language to Nepali but English is still display, and only restarting of app is necessary then you need to add following code in spinner.

Intent intent=getIntent();
overridePendingTransition(0, 0);
finish();
overridePendingTransition(0, 0);
startActivity(intent);

The above code will restart current activity. And that overridePendingTransition(0,0); removes the transitions annimation while open and closing of activity. Hope this helped!!

Queendevelopers
  • 183
  • 3
  • 20