0

I have implemented a Custom EditText which can take Bold, Italics, Underline text. Everything works fine except when I try to delete text by long pressing the backspace button. On long pressing backspace there is a delay in clearing the text.

Here is the overriden onTextChanged() method

protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {

    Log.d(VIEW_LOG_TAG,"Start: "+start+" Length before: "+lengthBefore+" Length After: "+lengthAfter+" TextLength: "+text.length());
    Spannable str = this.getText();
    CharacterStyle ss;
    UnderlineSpan ss1=null;
    int endLength = text.toString().length();

    switch (currentTypeface) {
        case TYPEFACE_NORMAL:
            ss = new StyleSpan(Typeface.NORMAL);
            break;
        case TYPEFACE_BOLD:
            ss = new StyleSpan(Typeface.BOLD);
            break;
        case TYPEFACE_ITALICS:
            ss = new StyleSpan(Typeface.ITALIC);
            break;
        case TYPEFACE_BOLD_ITALICS:
            ss = new StyleSpan(Typeface.BOLD_ITALIC);
            break;
        case TYPEFACE_UNDERLINE:
            ss= new UnderlineSpan();
            break;
        case TYPEFACE_BOLD_UNDERLINE:
            ss = new StyleSpan(Typeface.BOLD);
            ss1=new UnderlineSpan();
            break;
        case TYPEFACE_ITALICS_UNDERLINE:
            ss = new StyleSpan(Typeface.ITALIC);
            ss1=new UnderlineSpan();
            break;
        case TYPEFACE_BOLD_ITALICS_UNDERLINE:
            ss = new StyleSpan(Typeface.BOLD_ITALIC);
            ss1=new UnderlineSpan();
            break;
        default:
            ss = new StyleSpan(Typeface.NORMAL);
    }
        if(lastCursorPosition>endLength)
            return;
        Log.d(TextArea.class.getSimpleName(), new Integer(lastCursorPosition).toString() + new Integer(endLength).toString());
    if(ss1!=null)
        str.setSpan(ss1, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        str.setSpan(ss, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
logdev
  • 292
  • 6
  • 22

1 Answers1

1

That isn't a function of EditText, its a function of TextWatcher. You aren't making a custom EditText, you're trying to do a quick hack to avoid making a custom EditText. The difference is important because your technique lacks a lot of the power that creating a real custom edit text would have.

Secondly- are you sure the delay isn't in the keyboard? A typical technique for keys who's longpress is different than short press is to delay the action. Many keyboards even allow you to customize the delay (see Swype for example). So it may not be your code delaying it, its the keyboard's built in functionality. (I think this is the most likely answer).

Third- you're doing things really, really inefficiently. You should not be creating new Spans every time this is called. You should be creating 1 set of spans at creation time, and reusing them each time this is called. That alone would give you a good speedup if it is your performance that's the problem.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • It was the third one that resulted in the problem. I created them at the start and reused them as you said and it solved the problem. Thank you so much :) – logdev Jul 08 '17 at 09:31
  • Reusing spans solved the problem but created another one as I asked in this question https://stackoverflow.com/q/44998227/5045878. Please reply ;( – logdev Jul 09 '17 at 16:32