Skip to content
Advertisement

Creating a search box for quick entry in the spinner

The idea is what. Available:
Spiner, adapter and array from where the spinner receives data.
I would like to add a window to the spinner so that you can enter the first letters and the spinner, in addition to the entire list, would give out matches
An example of such a window:

enter image description here

Array and Spinner:

<Spinner
    android:id="@+id/spinner4"
    android:layout_width="271dp"
    android:layout_height="37dp"
    android:layout_marginTop="8dp"
    android:spinnerMode="dialog"
    app:layout_constraintStart_toStartOf="@+id/textView19"
    app:layout_constraintTop_toBottomOf="@+id/textView19"/>

Adapter:

public synchronized void fillNameStocks() {
        String[] tempArr = new String[arrayListMols.size() + 1];
        tempArr[0] = "Nothing";
        for (int i = 0; i < arrayListMols.size(); i++) {
            tempArr[i + 1] = arrayListMols.get(i).Name;
        }
        MyArrayAdapter<String> adapter = new MyArrayAdapter<String>(this, R.layout.spinner_item, tempArr);
        spinnerNamesOfMols.setAdapter(adapter);
        spinnerNamesOfMols.setSelection(0);
        spinnerNamesOfMols.setOnItemSelectedListener(new SpinnerActivity());
    }

Advertisement

Answer

Found on Google a library called, which allows just changing one line (write "com.toptoche.searchablespinnerlibrary.SearchableSpinner" instead of "Spinner") and that’s it, the problem is solved.

https://github.com/miteshpithadiya/SearchableSpinner

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement