'Android Studio | Spinner does not work on all smartphones
This code part of a method always causes the app to restart. It's not a crash, just a restart without any error messages. The curious thing, so far I have only discovered this behavior with Nokia 8 (Android 9) and Samsung Note 20 Ultra (Android 12).
The code is inside FragmentSettings, so it's the Settings menu item in the app. If the user makes any changes in it, the app will be restarted afterwards. With the smartphones mentioned above, the app is restarted directly as soon as the user wants to call up "Settings". So the user does not even have the opportunity to make changes.
I know this isn't a lot of code I'm showing here, but do you have any suspicions as to why this phenomenon is happening?
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
LocaleManager.setNewLocale(getContext(), countryToCode.get(spinner.getSelectedItem().toString()));
requireActivity().finish();
Intent i = new Intent(getActivity(), ActivityMain.class);
i.putExtra("RELOAD_VALUES", true);
startActivity(i);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
Solution 1:[1]
here is an example of how you change language in java
private Button langselect;
inside oncreate()
langselect = findViewById(R.id.selectlang);
langselect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showChangeLanguageDialog();
}
});
outside on create()
private void showChangeLanguageDialog(){
final String[] listItems = {"English","French"};
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(LoginActivity.this);
mBuilder.setTitle("Choose Language");
mBuilder.setSingleChoiceItems(listItems, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
if (i == 0 ){
setLocale("default");
recreate();
}
else if (i == 1){
setLocale("fr");
recreate();
}
dialog.dismiss();
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
}
private void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
editor.putString("MY_LANG",lang);
editor.apply();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
