-1

I get this error when i try to get the position of first and last character typed by a user in an AutoCompleteTextView.

I would appreciate any help provided.

When i add a textChangeListener i still get the error java.lang.IndexOutOfBoundsException: setSpan (0 ... -1) has end before start

This is my codes below:

final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocompleteView);

autoCompleteTextView.setAdapter(autoComplete);

autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        //my statements


    }
});

        autoCompleteTextView.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //int start = startText.indexOf(0);
            String startText = autoCompleteTextView.getText().toString();
            int end = startText.indexOf(1);
            SpannableStringBuilder builder = new SpannableStringBuilder(startText);
            // set foreground color (text color) - optional, you may not want to change the text color too
            builder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // set background color
            builder.setSpan(new BackgroundColorSpan(Color.YELLOW), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // set result to AutoCompleteTextView
            autoCompleteTextView.setText(builder);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Error Log

    01-31 16:02:37.418 20357-20357/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.lawerh.jonathan.partygoer, PID: 20357
                                               java.lang.IndexOutOfBoundsException: setSpan (0 ... -1) has end before start
                                                   at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1101)
                                                   at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:680)
                                                   at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:673)
                                                   at com.lawerh.jonathan.partygoer.ui.MapActivity$8.onTextChanged(MapActivity.java:328)
                                                   at android.widget.TextView.sendOnTextChanged(TextView.java:8320)
                                                   at android.widget.TextView.handleTextChanged(TextView.java:8385)
                                                   at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10531)
                                                   at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1051)
                                                   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:572)
                                                   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:503)
                                                   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:502)
                                                   at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:693)
                                                   at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:453)
                                                   at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:340)
                                                   at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
                                                   at android.os.Handler.dispatchMessage(Handler.java:111)
                                                   at android.os.Looper.loop(Looper.java:207)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5728)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
jonathan
  • 29
  • 1
  • 11

2 Answers2

1

int end = int end = startText.indexOf(1);

Is returning -1 because the number 1 isn't being found in the string. Then when you use end in the setSpan call, you are passing (0,-1). Double check that your startText contains a 1 and it may be that you just need to put the 1 in quotes, like this:

int end = int end = startText.indexOf("1");

Alex
  • 827
  • 8
  • 18
  • this works for me. but one more thing the Spannable method does not work. i am unable to highlight the text when i use the spannable method – jonathan Feb 01 '17 at 08:30
0

Problem is in int end = startText.indexOf(1); line of your code. First of all indexOf() function take character as an argument but you are passing 1 (integer) , and indexOf() function is returning -1 and when you are setting span builder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); then end value is -1, which throws IndexOutOfBoundsException

Mohd Asim Suhail
  • 2,176
  • 1
  • 16
  • 23
  • String q=""; int start = selectText.indexOf(q); int end = q.length()+start; but builder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); does not work for me – jonathan Feb 01 '17 at 08:31