Search in sources :

Example 1 with LanguagesAdapter

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(""));
}
Also used : EditText(android.widget.EditText) Locale(java.util.Locale) OnItemClickListener(android.widget.AdapterView.OnItemClickListener) HashMap(java.util.HashMap) Intent(android.content.Intent) MainActivity(fr.free.nrw.commons.contributions.MainActivity) View(android.view.View) AdapterView(android.widget.AdapterView) CampaignView(fr.free.nrw.commons.campaigns.CampaignView) ListView(android.widget.ListView) LanguagesAdapter(fr.free.nrw.commons.upload.LanguagesAdapter) ListView(android.widget.ListView) Dialog(android.app.Dialog) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable)

Aggregations

Dialog (android.app.Dialog)1 Intent (android.content.Intent)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 OnItemClickListener (android.widget.AdapterView.OnItemClickListener)1 EditText (android.widget.EditText)1 ListView (android.widget.ListView)1 CampaignView (fr.free.nrw.commons.campaigns.CampaignView)1 MainActivity (fr.free.nrw.commons.contributions.MainActivity)1 LanguagesAdapter (fr.free.nrw.commons.upload.LanguagesAdapter)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1