Skip to content Skip to sidebar Skip to footer

Illegalargumentexception While Selecting Text In Android Textview

I encountered a crash while deselecting text in a selectable Android TextView. This happens when I make a TextView selectable and set LinkMovementMethod. IllegalArgumentException i

Solution 1:

Thanks mariotaku for the answer. For some reason it wouldn't me me actually select the text in my TextView. I tweaked it a little to only trigger the work around when some characters are selected. It seems to be working great now!

@OverridepublicbooleandispatchTouchEvent(MotionEvent event) {
    // FIXME simple workaround to https://code.google.com/p/android/issues/detail?id=191430intstartSelection= getSelectionStart();
    intendSelection= getSelectionEnd();
    if (startSelection != endSelection) {
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            finalCharSequencetext= getText();
            setText(null);
            setText(text);
        }
    }
    returnsuper.dispatchTouchEvent(event);
}

Solution 2:

I found Android will clear TextView selection when calling setText, so here's a simple workaround to this problem.

publicclassFixedTextViewextendsTextView {

    publicFixedTextView(final Context context) {
        super(context);
    }

    publicFixedTextView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    publicFixedTextView(final Context context, final AttributeSet attrs, finalint defStyle) {
        super(context, attrs, defStyle);
    }

    @OverridepublicbooleandispatchTouchEvent(MotionEvent event) {
        // FIXME simple workaround to https://code.google.com/p/android/issues/detail?id=191430if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            finalCharSequencetext= getText();
            setText(null);
            setText(text);
        }
        returnsuper.dispatchTouchEvent(event);
    }

}

Post a Comment for "Illegalargumentexception While Selecting Text In Android Textview"