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!!