0
votes

I'm currently in the making of an android application, with autocomplete from some fixed strings array.

When the user press on the auto complete text view, I want that a menu with "current location" option will be opened as default (before the user even started to type), but I'm not using Google Places (as the values of the auto-complete is from the array), so how can I do this? Thank you very much!

2

2 Answers

3
votes

What you want to show is a default suggestion in your AutocompleteTextView.

suppose you have an AutocompleteTextView named locationAutoComplete. You can show default suggestion by

  locationAutoComplete.setText("current location");

But there is a problem in AutocompleteTextview to show default suggestion using setText(String str); method directly. Whenever setText(String str) method is called it disables the AutocompleteTextView.

To prevent it write your code as below.

    locationAutoComplete.postDelayed(new Runnable() {
        @Override
        public void run() {
            locationAutoComplete.showDropDown();
        }
    },500);
    locationAutoComplete.setText("current location");
    locationAutoComplete.setSelection(locationAutoComplete.getText().length());
1
votes

If you are only using a fixed set of strings, you can use AutoCompleteTextView:

String[] yourStrings; // However you get your strings

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