Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete text in android?

Tags:

android

In my application I am using an auto-complete-text. When the user types t it shows Three , Two , fifty but I want to show Two , Three not fifty. Can any one help me to solve this problem.

String search[] = {"one","two","three","four","five","six","seven","fifty"};    

ArrayAdapter<String> adp= new ArrayAdapter<String>(this,R.layout.searchtext, search);
    search_item.setThreshold(1);
    search_item.setAdapter(adp);
like image 633
Yugesh Avatar asked Apr 05 '13 11:04

Yugesh


1 Answers

i don't think you have to create custom adapter. its default functionality which you want. check this

here i have array like:

private static final String[] COUNTRIES = new String[] { "Belgium",
        "France", "France_", "Italy", "Germany", "Spain","abcf","FFa","FFb","bFF","aFF" };

when i search ff then its give me only 2("FFa","FFb") suggestion not 4("FFa","FFb","bFF","aFF")

enter image description here

i have used this in ActionBar. you can also use this in layout. because its custom layout in ActionBar.

public class TestActivity extends Activity {

    /** Called when the activity is first created. */
    private static final String[] COUNTRIES = new String[] { "Belgium",
        "France", "France_", "Italy", "Germany", "Spain","abcf","FFa","FFb","bFF","aFF" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        // actionBar.setDisplayShowTitleEnabled(false);
        // actionBar.setIcon(R.drawable.ic_action_search);

        LayoutInflater inflator = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.test, null);

        actionBar.setCustomView(v);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);
        AutoCompleteTextView textView = (AutoCompleteTextView) v
                .findViewById(R.id.editText1);
        textView.setAdapter(adapter);

    }
}

test.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Action Bar:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

    <AutoCompleteTextView
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:imeOptions="actionSearch"
        android:inputType="textAutoComplete|textAutoCorrect"
        android:textColor="#FFFFFF" >

        <requestFocus />
    </AutoCompleteTextView>

</LinearLayout>
like image 98
Dhaval Parmar Avatar answered Oct 25 '22 21:10

Dhaval Parmar