How get coordinate of a ClickableSpan inside a TextView?
I have found a solution :-)Here it is, may be this will help others with the same problem like me !
// Initialize global valuethis.parentTextViewRect = new Rect(); // Initialize values for the computing of clickedText positionSpannableString completeText = (SpannableString)(parentTextView).getText();Layout textViewLayout = parentTextView.getLayout(); double startOffsetOfClickedText = completeText.getSpanStart(clickedText);double endOffsetOfClickedText = completeText.getSpanEnd(clickedText);double startXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal((int)startOffsetOfClickedText);double endXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal((int)endOffsetOfClickedText); // Get the rectangle of the clicked textint currentLineStartOffset = textViewLayout.getLineForOffset((int)startOffsetOfClickedText);int currentLineEndOffset = textViewLayout.getLineForOffset((int)endOffsetOfClickedText);boolean keywordIsInMultiLine = currentLineStartOffset != currentLineEndOffset;textViewLayout.getLineBounds(currentLineStartOffset, this.parentTextViewRect); // Update the rectangle position to his real position on screenint[] parentTextViewLocation = {0,0};parentTextView.getLocationOnScreen(parentTextViewLocation); double parentTextViewTopAndBottomOffset = ( parentTextViewLocation[1] - parentTextView.getScrollY() + this.parentTextView.getCompoundPaddingTop());this.parentTextViewRect.top += parentTextViewTopAndBottomOffset;this.parentTextViewRect.bottom += parentTextViewTopAndBottomOffset; // In the case of multi line text, we have to choose what rectangle takeif (keywordIsInMultiLine){ int screenHeight = this.mWindowManager.getDefaultDisplay().getHeight(); int dyTop = this.parentTextViewRect.top; int dyBottom = screenHeight - this.parentTextViewRect.bottom; boolean onTop = dyTop > dyBottom; if (onTop){ endXCoordinatesOfClickedText = textViewLayout.getLineRight(currentLineStartOffset); } else{ this.parentTextViewRect = new Rect(); textViewLayout.getLineBounds(currentLineEndOffset, this.parentTextViewRect); this.parentTextViewRect.top += parentTextViewTopAndBottomOffset; this.parentTextViewRect.bottom += parentTextViewTopAndBottomOffset; startXCoordinatesOfClickedText = textViewLayout.getLineLeft(currentLineEndOffset); } } this.parentTextViewRect.left += ( parentTextViewLocation[0] + startXCoordinatesOfClickedText + this.parentTextView.getCompoundPaddingLeft() - parentTextView.getScrollX());this.parentTextViewRect.right = (int) ( this.parentTextViewRect.left + endXCoordinatesOfClickedText - startXCoordinatesOfClickedText);
try this:
inside of CustomSpannableString class onClick function
@Overridepublic void onClick(View v){ val textView = v as TextView val s = textView.text as Spanned val startCoordinates = s.getSpanStart(this) val endCoordinates = s.getSpanEnd(this) return arrayOf(startCoordinates, endCoordinates)}
not in spannableString class
val textView = findView... // txtView which text is set to SpannableStringval s = textView.text as Spanned // CustomSpannableStringObj of type CustomSpannableString val startCoordinates = s.getSpanStart(CustomSpannableStringObj)val endCoordinates = s.getSpanEnd(CustomSpannableStringObj)