protected void DispatchUrlClick(TextView textView, ClickableSpanWithText spanWithText) { var spanUrl = spanWithText.Text; var handled = _onLinkClickListener != null && _onLinkClickListener.OnClick(textView, spanUrl); if (!handled) { // Let Android handle this click. spanWithText.Span.OnClick(textView); } }
/** * Adds a highlight background color span to the TextView. */ protected void HighlightUrl(TextView textView, ClickableSpanWithText spanWithText, ISpannable text) { if (_isUrlHighlighted) { return; } _isUrlHighlighted = true; var spanStart = text.GetSpanStart(spanWithText.Span); var spanEnd = text.GetSpanEnd(spanWithText.Span); text.SetSpan(new BackgroundColorSpan(textView.HighlightColor), spanStart, spanEnd, SpanTypes.InclusiveInclusive); textView.SetText(text, TextView.BufferType.Spannable); Selection.SetSelection(text, spanStart, spanEnd); }
/** * Determines the touched location inside the TextView's text and returns the ClickableSpan found under it (if any). * * @return The touched ClickableSpan or null. */ protected ClickableSpanWithText FindClickableSpanUnderTouch(TextView textView, ISpannable text, MotionEvent e) { // So we need to find the location in text where touch was made, regardless of whether the TextView // has scrollable text. That is, not the entire text is currently visible. var touchX = (int)e.GetX(); var touchY = (int)e.GetY(); // Ignore padding. touchX -= textView.TotalPaddingLeft; touchY -= textView.TotalPaddingTop; // Account for scrollable text. touchX += textView.ScrollX; touchY += textView.ScrollY; var layout = textView.Layout; var touchedLine = layout.GetLineForVertical(touchY); var touchOffset = layout.GetOffsetForHorizontal(touchedLine, touchX); _touchedLineBounds.Left = layout.GetLineLeft(touchedLine); _touchedLineBounds.Top = layout.GetLineTop(touchedLine); _touchedLineBounds.Right = layout.GetLineWidth(touchedLine) + _touchedLineBounds.Left; _touchedLineBounds.Bottom = layout.GetLineBottom(touchedLine); if (_touchedLineBounds.Contains(touchX, touchY)) { // Find any ClickableSpan that lies under the touched area var spans = text.GetSpans(touchOffset, touchOffset, SpanClass); foreach (var span in spans) { if (span is ClickableSpan) { return(ClickableSpanWithText.OfSpan(textView, span as ClickableSpan)); } } // No ClickableSpan found under the touched location. return(null); } // Touch lies outside the line's horizontal bounds where no spans should exist. return(null); }