AutoCompleteTextView-
An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop-down menu from which the user can choose an item to replace the content of the edit box with. The drop-down list automatically dismisses when back key press. AutoCompleteTextView Auto fill the item when the user clicks the item of the drop-down list.
The following snippet to add it to your layout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<AutoCompleteTextView android:id="@+id/autoCompleteTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:textColorHint="@color/white_color" android:textColor="@color/white_color" android:background="@drawable/rect_shape" android:maxLines="1" android:lines="1" android:singleLine="true" android:ellipsize="end" android:imeOptions="actionSearch" android:inputType="textAutoComplete|textAutoCorrect" android:hint="Enter Place Here"/> |
We can add a threshold value (a number of characters) to start suggestion list while the user reached the threshold value (input the no. of characters).
1 |
mAutocompleteTextView.setThreshold(3); |
Add array adapter to AutoCompleteTextView.
1 2 3 |
PlaceArrayAdapter mPlaceArrayAdapter = new PlaceArrayAdapter(this, R.layout.item_search_location, BOUNDS_MOUNTAIN_VIEW, null); mAutocompleteTextView.setAdapter(mPlaceArrayAdapter); |
Adding Search Feature-
We can add the search icon to keyboard and handle them when the user clicks the search icon. the following snipped describe how to add the search feature to AutoCompleteTextView.
Firstly, We need to add search action to AutoCompleteTextView layout-
1 |
android:imeOptions="actionSearch" |
Now we need to add OnEditorActionListener when the user clicks the search icon on the keyboard then this listener will fire and execute onEditorAction function code. following snipped for the listener.
1 2 3 4 5 6 7 8 9 10 11 |
mAutocompleteTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { callWebService(); hideSoftKeyboard(textView); return true; } return false; } }); |
Performing Item Click Action-
You can perform some events as you need when User Clicks the drop-down Item. the following snippet shows the hide the keyboard and adds the Listener with Place result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
private AdapterView.OnItemClickListener mAutocompleteClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { final PlaceArrayAdapter.PlaceAutocomplete item = mPlaceArrayAdapter.getItem(position); final String placeId = String.valueOf(item.placeId); PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi .getPlaceById(mGoogleApiClient, placeId); placeResult.setResultCallback(mUpdatePlaceDetailsCallback); // Hide key Board View view2 = MapActivity.this.getCurrentFocus(); if (view2 != null) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view2.getWindowToken(), 0); } } }; |
How it looks-
References:- https://developer.android.com/reference/android/widget/AutoCompleteTextView