Code example for Spannable

Methods: getSpanEnd, getSpanStart, getSpans, removeSelection, setSelection

0
 
            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);
 
            ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
 
            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    link[0].onClick(widget);
                } else if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
                } 
 
                return true; 
            } else { 
                this.removeSelection(buffer);
            } 
        } 
 
        return super.onTouchEvent(widget, buffer, event);
    } 
 
    @Override 
    public void initialize(TextView widget, final Spannable text) {
        widget.setOnLongClickListener(new TextView.OnLongClickListener() {
 
            @Override 
            public boolean onLongClick(View v) {
                MyLinkMovementMethod.this.mSkipNextClick = true;
                MyLinkMovementMethod.this.removeSelection(text);
                return false; 
            } 
        }); 
        this.removeSelection(text);
        text.removeSpan(FROM_BELOW);
    } 
 
    @Override 
    public void onTakeFocus(TextView view, Spannable text, int dir) {
        this.removeSelection(text);
 
        if ((dir & View.FOCUS_BACKWARD) != 0) {
            text.setSpan(FROM_BELOW, 0, 0, Spanned.SPAN_POINT_POINT);
        } else { 
            text.removeSpan(FROM_BELOW);
        } 
    } 
 
    public static MyLinkMovementMethod getInstance() { 
        if (sInstance == null) {
            sInstance = new MyLinkMovementMethod();
        } 
 
        return sInstance;
    } 
     
    private void removeSelection(Spannable text) {
        Selection.removeSelection(text);
    } 
 
    private static MyLinkMovementMethod sInstance;
    private static Object FROM_BELOW = new NoCopySpan.Concrete();
}