Parthiban M
Parthiban M

Reputation: 1104

To customize the list of an AutocompleteTextView

Hi I'm trying to create an AutoCompleteTextView. The resulting list of this AutoCompleteTextView should be placed below the dit text. Can anyone please help me how I can achieve this?

Upvotes: 0

Views: 613

Answers (2)

siriscac
siriscac

Reputation: 1707

Looking at the question, it looks like you need to show the results in a separate list. So, Assuming your EditText is called as searchText and assuming your ListAdapter is called as searchAdapter, Then set addTextChangedListener to the EditText.

EditText searchText = (EditText) findViewById(R.id.search);
searchText.addTextChangedListener(new TextWatcher() {
       public void afterTextChanged(Editable s) {

       }

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

       }

       public void onTextChanged(CharSequence s, int start, int before, int count) {
           searchAdapter.filter(s.toString());
       }
});

And then in the ListAdapter, add a filter function to filter from the dataset using the keyword entered.

private List<String> titles, titlesCopy;
//keep a copy of your dataset
public void filter(String charText) {
   charText = charText.toLowerCase(Locale.getDefault());
   titles.clear();
   if (charText.length() == 0) {
      titles.addAll(titlesCopy); //no text entered, so add all results
   } else {
      for (String filtered : titlesCopy) { //check with keyword from the copy data set
          if (filtered.toLowerCase(Locale.getDefault()).contains(charText)) {
                titles.add(filtered);
          }
      }
   }
   notifyDataSetChanged();
}

Upvotes: 2

Hawraa Khalil
Hawraa Khalil

Reputation: 261

Try something like this:

<AutoCompleteTextView
    android:id="@+id/txtPurpose"
    android:layout_width="370dp"
    android:layout_height="50dp"
    android:dropDownHeight="300dp"
    android:paddingStart="10dp"
    android:paddingEnd="10dp"/>

and use android.R.layout.simple_dropdown_item_1line, as a design for the list that you want to fill

I hope this will help you solving what you want, and let me know if it didn't work.

Upvotes: 0

Related Questions