Example usage for android.text TextWatcher afterTextChanged

List of usage examples for android.text TextWatcher afterTextChanged

Introduction

In this page you can find the example usage for android.text TextWatcher afterTextChanged.

Prototype

public void afterTextChanged(Editable s);

Source Link

Document

This method is called to notify you that, somewhere within s, the text has been changed.

Usage

From source file:de.gebatzens.sia.dialog.FilterDialog.java

@Override
public void onResume() {
    super.onResume();

    final AlertDialog d = (AlertDialog) getDialog();

    final Filter.FilterList list = SIAApp.SIA_APP.filters;
    final AutoCompleteTextView ed = (AutoCompleteTextView) d.findViewById(R.id.filter_text);
    final AppCompatSpinner spinner = (AppCompatSpinner) d.findViewById(R.id.filter_spinner);
    final CheckBox cb = (CheckBox) d.findViewById(R.id.checkbox_contains);
    final TextView label = (TextView) d.findViewById(R.id.filter_label);
    final TextWatcher watcher;
    ed.setSelectAllOnFocus(true);/*from  w w  w.j  a  va  2s.  c  o  m*/

    if (isMainFilterDialog()) {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),
                R.layout.support_simple_spinner_dropdown_item,
                new String[] { getString(R.string.school_class), getString(R.string.teacher) });
        spinner.setAdapter(adapter);

        if (getMainFilterPosition() == -1) {
            ed.setText("");
            Subst.GGPlans plans = (Subst.GGPlans) SIAApp.SIA_APP.school.fragments
                    .getByType(FragmentData.FragmentType.PLAN).get(0).getData();

            //could confuse people
            /*if(plans != null) {
            ed.setAdapter(new ArrayAdapter<String>(ed.getContext(), android.R.layout.simple_dropdown_item_1line, plans.getAllClasses()));
            }*/

            spinner.setSelection(0);
        } else {
            ed.setText(list.including.get(getMainFilterPosition()).getFilter());
            spinner.setSelection(
                    list.including.get(getMainFilterPosition()).getType() == Filter.FilterType.CLASS ? 0 : 1);
        }

        cb.setVisibility(View.GONE);
        label.setVisibility(View.GONE);

    } else {
        ed.setHint(getString(R.string.subject_course_name));
        ArrayAdapter<Filter.IncludingFilter> adapter = new ArrayAdapter<>(getContext(),
                R.layout.support_simple_spinner_dropdown_item);
        adapter.addAll(list.including);
        spinner.setAdapter(adapter);

        cb.setEnabled(true);
        cb.setText(getString(R.string.all_subjects_including_disabled));

        int p = getUpdatePosition();
        if (p != -1) {
            Filter.ExcludingFilter f = list.including.get(getMainFilterPosition()).excluding
                    .get(getUpdatePosition());

            cb.setChecked(f.contains);
            cb.setText(getString(R.string.all_subjects_including, f.getFilter()));
            ed.setText(f.getFilter());

            label.setText(f.getParentFilter().toString());
            label.setVisibility(View.VISIBLE);
            spinner.setVisibility(View.GONE);
        } else {
            label.setVisibility(View.GONE);
        }
    }

    ed.addTextChangedListener(watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String str = s.toString().trim();

            int op = isMainFilterDialog() ? getMainFilterPosition() : getUpdatePosition();
            List<? extends Filter> flist = isMainFilterDialog() ? list.including
                    : list.including.get(spinner.getSelectedItemPosition()).excluding;
            Filter.FilterType currentType = isMainFilterDialog()
                    ? (spinner.getSelectedItemPosition() == 0 ? Filter.FilterType.CLASS
                            : Filter.FilterType.TEACHER)
                    : Filter.FilterType.SUBJECT;

            for (int i = 0; i < flist.size(); i++) {
                if (i == op && (!isMainFilterDialog() || flist.get(op).getType() == currentType)) {
                    continue;
                }

                Filter f = flist.get(i);

                if (f.getType() == currentType && SIAApp.deleteNonAlphanumeric(f.getFilter())
                        .equals(SIAApp.deleteNonAlphanumeric(str))) {
                    ed.setError(getString(R.string.filter_exists));
                    d.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
                } else {
                    //ed.setError(null);
                    d.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
                }
            }

            if (!isMainFilterDialog()) {

                if (str.length() == 0) {
                    cb.setEnabled(false);
                    cb.setText(getString(R.string.all_subjects_including_disabled));
                } else {
                    cb.setEnabled(true);
                    cb.setText(getString(R.string.all_subjects_including, str));
                }
            }
        }
    });

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (isMainFilterDialog()) {
                ed.setHint((String) spinner.getSelectedItem());
            }

            watcher.afterTextChanged(ed.getText());
            //ed.setError(null);
            //ed.setText(ed.getText());

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}