I've a MultiAutoCompleteTextView
custom control in which I am creating chips text, when user presses the space key.
I do not want the user to input space initially when the textbox is empty so I put an inputFilter
in order to prevent user from putting space initially.
Here's the Filter Code:
private void RestrictUselessSpaces(){
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isWhitespace(source.charAt(i))) {
if(!getText().toString().trim().equals(""))
{
return " ";
}
else
{
return "";
}
}
}
return null;
}
};
setFilters(new InputFilter[]{filter});
}
Somehow when I comment RestrictUselessSpaces
function. It works fine. But when this function runs. Than if I try to enter space after a character. It gives me the indexoutofboundexception
error. Here's my code where I get the error.
public void setChips(String s){
if(s.contains(" ") && !s.trim().equals("")) // check space in string
{
this.chips = s.trim().split(" ");
SpannableStringBuilder ssb = new SpannableStringBuilder(getText());
// split string wich comma
int x =0;
// loop will generate ImageSpan for every country name separated by comma
for(String c : chips){
// inflate chips_edittext layout
LayoutInflater lf = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
TextView textView = (TextView) lf.inflate(R.layout.chips_edittext, null);
textView.setText(c); // set text
setFlags(textView, c); // set flag image
// capture bitmapt of genreated textview
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(spec, spec);
textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
canvas.translate(-textView.getScrollX(), -textView.getScrollY());
textView.draw(canvas);
textView.setDrawingCacheEnabled(true);
Bitmap cacheBmp = textView.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
textView.destroyDrawingCache(); // destory drawable
// create bitmap drawable for imagespan
BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
bmpDrawable.setBounds(0, 0,bmpDrawable.getIntrinsicWidth(),bmpDrawable.getIntrinsicHeight());
// create and set imagespan
ssb.setSpan(new ImageSpan(bmpDrawable),x ,x + c.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
x = x+ c.length() +1;
}
// set chips span
setText(ssb);
// move cursor to last
setSelection(s.length());
}
}
The line ssb.setSpan
is causing the problem but when I debug the code. The debugger runs this line without giving me any kind of exception. Error occurs after the execution of this function which is making it more hard for me to identify what actually is causing the problem.
And here's my logcat:
08-10 20:04:19.253: E/AndroidRuntime(18461): FATAL EXCEPTION: main
08-10 20:04:19.253: E/AndroidRuntime(18461): Process: com.example.twitter, PID: 18461
08-10 20:04:19.253: E/AndroidRuntime(18461): java.lang.IndexOutOfBoundsException: setSpan (2 ... 2) ends beyond length 1
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.Selection.setSelection(Selection.java:76)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.Selection.setSelection(Selection.java:87)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.widget.EditText.setSelection(EditText.java:94)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.example.libraries.ChipsMultiAutoCompleteTextview.setChips(ChipsMultiAutoCompleteTextview.java:281)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.example.libraries.ChipsMultiAutoCompleteTextview$1.onTextChanged(ChipsMultiAutoCompleteTextview.java:192)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.widget.TextView.sendOnTextChanged(TextView.java:7429)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.widget.TextView.handleTextChanged(TextView.java:7488)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9203)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:675)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:197)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.os.Handler.dispatchMessage(Handler.java:102)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.os.Looper.loop(Looper.java:212)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.app.ActivityThread.main(ActivityThread.java:5151)
08-10 20:04:19.253: E/AndroidRuntime(18461): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 20:04:19.253: E/AndroidRuntime(18461): at java.lang.reflect.Method.invoke(Method.java:515)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
08-10 20:04:19.253: E/AndroidRuntime(18461): at dalvik.system.NativeStart.main(Native Method)
try using this
setSelection(s.length() - 1);