use of fr.free.nrw.commons.upload.LanguagesAdapter in project apps-android-commons by commons-app.
the class SettingsFragment method prepareAppLanguages.
/**
* Prepare and Show language selection dialog box
* Uses previously saved language if there is any, if not uses phone locale as initial language.
* Disable default/already selected language from dialog box
* Get ListPreference key and act accordingly for each ListPreference.
* saves value chosen by user to shared preferences
* to remember later and recall MainActivity to reflect language changes
* @param keyListPreference
*/
private void prepareAppLanguages(final String keyListPreference) {
// Gets current language code from shared preferences
final String languageCode = getCurrentLanguageCode(keyListPreference);
HashMap<Integer, String> selectedLanguages = new HashMap<>();
if (keyListPreference.equals("appUiDefaultLanguagePref")) {
assert languageCode != null;
if (languageCode.equals("")) {
selectedLanguages.put(0, Locale.getDefault().getLanguage());
} else {
selectedLanguages.put(0, languageCode);
}
} else if (keyListPreference.equals("descriptionDefaultLanguagePref")) {
assert languageCode != null;
if (languageCode.equals("")) {
selectedLanguages.put(0, Locale.getDefault().getLanguage());
} else {
selectedLanguages.put(0, languageCode);
}
}
LanguagesAdapter languagesAdapter = new LanguagesAdapter(getActivity(), selectedLanguages);
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_select_language);
dialog.setCanceledOnTouchOutside(true);
dialog.getWindow().setLayout((int) (getActivity().getResources().getDisplayMetrics().widthPixels * 0.90), (int) (getActivity().getResources().getDisplayMetrics().heightPixels * 0.90));
dialog.show();
EditText editText = dialog.findViewById(R.id.search_language);
ListView listView = dialog.findViewById(R.id.language_list);
listView.setAdapter(languagesAdapter);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
languagesAdapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String languageCode = ((LanguagesAdapter) adapterView.getAdapter()).getLanguageCode(i);
saveLanguageValue(languageCode, keyListPreference);
Locale defLocale = new Locale(languageCode);
if (keyListPreference.equals("appUiDefaultLanguagePref")) {
appUiLanguageListPreference.setSummary(defLocale.getDisplayLanguage(defLocale));
setLocale(Objects.requireNonNull(getActivity()), languageCode);
getActivity().recreate();
final Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
} else {
descriptionLanguageListPreference.setSummary(defLocale.getDisplayLanguage(defLocale));
}
dialog.dismiss();
}
});
dialog.setOnDismissListener(dialogInterface -> languagesAdapter.getFilter().filter(""));
}
Aggregations