How to add space to an AutoCompleteTextView clause

I would like to add a space when the user selects text from sentences with automatic completion, so when he continues to enter text, he will start with a new word.

I tried to do this using TextWatcher , but I get an IndexOutOfBoundsException .

Any suggestions?

enter image description here

text watcher I use:

  private class AddSpaceTextWatcher implements TextWatcher{ boolean shouldAddSpace = false; @Override public void beforeTextChanged(CharSequence s, int start, int before, int count) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (count - before > 1) // check that the new input is not from the keyboard shouldAddSpace = true; } @Override public void afterTextChanged(Editable editable) { if (shouldAddSpace) { shouldAddSpace = false; mAutoCompleteTextView.setText(" "); } } } 

An exception I get:

 java.lang.IndexOutOfBoundsException: setSpan (18 ... 18) ends beyond length 1 at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1018) at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:611) at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:607) at android.text.Selection.setSelection(Selection.java:76) at android.text.Selection.setSelection(Selection.java:87) at android.widget.EditText.setSelection(EditText.java:99) at android.widget.SearchView.setQuery(SearchView.java:1465) at android.widget.SearchView.onQueryRefine(SearchView.java:889) at android.widget.SuggestionsAdapter.onClick(SuggestionsAdapter.java:371) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19748) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 
+7
android searchview autocompletetextview
source share
4 answers

To select multiple items, you can simply use MultiAutoCompleteTextView , which is a subclass of AutoCompleteTextView specifically created for this purpose.

OR

If you just want to add a place at the end to select only one item, than try:

 public void afterTextChanged(Editable editable) { String text = editable.toString(); if (shouldAddSpace && text.length() > 0 && !text.endsWith(" ")) { editable.append(' '); } } 

Edit:

Once space is added, you can simply call mAutoCompleteTextView.showDropDown(); to reopen popup text with updated text.

+3
source share

I'm not sure if this solves your problem, but mAutoCompleteTextView.setText(" "); will delete the text. So I would do the following:

 mAutoCompleteTextView.setText(mAutoCompleteTextView.getText().toString() + " "); 
0
source share
  mAutoCompleteTextView.setText(" "); 

This code sets the text of the text view to "" - what you want is the existing text plus a space.

 mAutoCompleteTextView.setText(mAutoCompleteTextView.getText().append(" ")); 
0
source share

I prefer to use "MultiAutoCompleteTextView" to achieve what you are trying to do.

 <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="65dp" android:ems="10" > 

In java file:

 private AutoCompleteTextView actv; actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); 

List of lines:

  String[] countries = getResources(). getStringArray(R.array.list_of_countries); ArrayAdapter adapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,countries); actv.setAdapter(adapter); 

This example displays a list of countries matching user input. I hope this example helps you in your work.

0
source share

All Articles