Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoCompleteTextView Filter not working properly

Tags:

java

android

I have an AutoCompleteTextView and I want to set a filter on it, the problem I'm having is when I go to type something it'll only let me type 1 character in the text field. if I remove textView.setFilters from the code it works fine I just don't have any filters. I have also tried android:textAllCaps="true" in my xml file and it doesn't work. any help would be great, thanks.

My code:

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.textfield);
    String[] MyArray = getResources().getStringArray(R.array.myarray);
    ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, MyArray);
    textView.setFilters(new InputFilter[]{new InputFilter.AllCaps(), new InputFilter.LengthFilter(40)});
    textView.setAdapter(adapter);

Xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".myactivity">

    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textfield"
        android:hint="Search..."/>

    <Button
        android:id="@+id/btn"
        android:onClick="buttonClickHandler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Do something"
        android:layout_toRightOf="@+id/textfield"/>
</RelativeLayout>
like image 207
Greg432 Avatar asked Sep 16 '25 23:09

Greg432


1 Answers

With the help of @pskink's post I figured it out

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.textfield);
String[] MyArray = getResources().getStringArray(R.array.myarray);
ArrayAdapter<String> adapter =
        new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, MyArray);
        textView.setThreshold(1);
        InputFilter[] filters = {
            new InputFilter.AllCaps(),
            new InputFilter.LengthFilter(40),};
    textView.setFilters(filters);
textView.setAdapter(adapter);
like image 90
Greg432 Avatar answered Sep 18 '25 17:09

Greg432