0

I have a textview with link included as an a tag. In string.xml I have string like this - This is my text view with <a href="url">link</a>

I want to programmatically check whether textview has this link and whether it is clickable. If I try using textview.getText().toString() it will convert html to plain text (not raw html) and remove all the links.

Is there any way I can check this? The second part is, I can manually verify that the link is clickable, but I want to programmatically verify whether the link is working. How do I do that?

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
  • 1
    `textview.getUrls()` gets you the spannable URLs in the text view - so in your example it would return an array of length 1 and `[0].getUrl()` would return "url". –  Jul 26 '20 at 20:30
  • 1
    For the second part you may need to define what "working" means - is it just a 200 HTTP response code? –  Jul 26 '20 at 20:36
  • I want to make sure it is clickable -- setmovementmethod is properly invoked on textview. – Mayank Kumar Chaudhari Jul 26 '20 at 21:21
  • Did you try using android linkyfy. Try this. Might help you. Sorry if I mislead you by this. https://developer.android.com/reference/android/text/util/Linkify – KalanaChinthaka Jul 27 '20 at 02:05

1 Answers1

1

Thanks to @Andy

textview.getUrls() gets you the spannable URLs in the text view - so in your example it would return an array of length 1 and [0].getUrl() would return "url".

((TextView) v).getUrls()[0].getURL()
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122