Example usage for android.widget ArrayAdapter getFilter

List of usage examples for android.widget ArrayAdapter getFilter

Introduction

In this page you can find the example usage for android.widget ArrayAdapter getFilter.

Prototype

@Override
    public @NonNull Filter getFilter() 

Source Link

Usage

From source file:com.github.dfa.diaspora_android.activity.PodSelectionActivity.java

private void setListedPods(String[] listedPodsArr) {
    final ArrayList<String> listedPodsList = new ArrayList<>();
    for (String pod : listedPodsArr) {
        listedPodsList.add(pod.toLowerCase());
    }/*  www.j a  v a  2  s  .c o m*/

    final ArrayAdapter<String> adapter = new ArrayAdapter<>(PodSelectionActivity.this,
            android.R.layout.simple_list_item_1, listedPodsList);

    // save index and top position
    int index = listPods.getFirstVisiblePosition();
    View v = listPods.getChildAt(0);
    int top = (v == null) ? 0 : (v.getTop() - listPods.getPaddingTop());
    listPods.setAdapter(adapter);
    listPods.setSelectionFromTop(index, top);

    adapter.getFilter().filter(editFilter.getText());
    editFilter.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            (adapter).getFilter().filter(s.toString());
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
}

From source file:cz.maresmar.sfm.view.WithExtraFragment.java

@SuppressLint("ClickableViewAccessibility")
@UiThread/* w w w.ja  v a  2 s  . co  m*/
private void inflateExtraFormat() {
    // Remove old extras from UI
    mExtraLinearLayout.removeAllViewsInLayout();

    mExtraUiBindings = new EditText[mExtrasFormat.size()];

    final LinearLayout.LayoutParams matchWrapParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    // For each extra
    int index = 0;
    for (ExtraFormat extraFormat : mExtrasFormat) {
        //noinspection ConstantConditions
        TextInputLayout textInputLayout = new TextInputLayout(getContext());
        textInputLayout.setLayoutParams(matchWrapParams);
        textInputLayout.setHint(extraFormat.name);

        // If edit text extra
        if (extraFormat.valuesList.length == 0) {
            TextInputEditText editText = new TextInputEditText(getContext());
            editText.setLayoutParams(matchWrapParams);

            mExtraUiBindings[index] = editText;

            textInputLayout.addView(editText);
        } else { // If extra with dropdown
            // Prepare adapter for values
            ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),
                    R.layout.support_simple_spinner_dropdown_item, extraFormat.valuesList);

            AutoCompleteTextView autoCompleteTextView = new AutoCompleteTextView(getContext());
            autoCompleteTextView.setLayoutParams(matchWrapParams);
            autoCompleteTextView.setAdapter(adapter);
            // Some UI tweaks to make it look nice
            autoCompleteTextView.setKeyListener(null);
            autoCompleteTextView.setOnTouchListener((v, event) -> {
                ((AutoCompleteTextView) v).showDropDown();
                return false;
            });
            // Set default value
            autoCompleteTextView.setText(extraFormat.valuesList[0]);
            // setText disable other values so I have un-filter them
            adapter.getFilter().filter(null);

            mExtraUiBindings[index] = autoCompleteTextView;

            textInputLayout.addView(autoCompleteTextView);
        }
        mExtraLinearLayout.addView(textInputLayout);

        // Adds optimal extra description
        if (extraFormat.description != null) {
            TextView description = new TextView(getContext());
            description.setText(extraFormat.description);
            TextViewCompat.setTextAppearance(description, R.style.StaticLabel);

            LinearLayout.LayoutParams descriptionLayoutParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            descriptionLayoutParams.setMargins(getResources().getDimensionPixelSize(R.dimen.content_margin), 0,
                    0, 0);
            description.setLayoutParams(descriptionLayoutParams);

            mExtraLinearLayout.addView(description);
        }

        index++;
    }
}