3

We can use a TextViewfor adding hyperlinks to it by various methods, like using the attribute autoLink, or by using setMovementMethod().

Can we do the same using and EditText widget? I am trying to create a notepad, in which if any such text entered, like url, email, number or something similar, we should get a hyperlink to click on it and open the browser.

Please help.

Thanks All.

user4232
  • 592
  • 1
  • 12
  • 37
Neel
  • 77
  • 7
  • 2
    An EditText implies that the user is editing the text; having some words as active links at that moment would be a hindrance to editing. Think about what would happen if a user clicked a word that was an active link - how would he edit it? – Raghunandan Apr 09 '13 at 07:53
  • kk i got it thanx for help ... – Neel Apr 09 '13 at 08:45

1 Answers1

2

As of Android API level 8 there is a WEB_URL pattern. Quoting the source, it "match[es] most part of RFC 3987". If you target a lower API level you could simply copy the pattern from the source and include it in your application. I assume you know how to use patterns and matchers, so I'm not going into more details here.

Also the class URLUtil provides some useful methods, e.g:

isHttpUrl() isValidUrl() The descriptions of the methods are not very elaborate, therefore you are probably best of looking at the source and figuring out which one fits your purpose best.

As for when to trigger the validation check, there are multiple possibilities: you could use the EditText callback functions

onFocusChanged(), or onTextChanged() or use a TextWatcher, which I think would be better.

I hope this helps, best regards,

Rohit
  • 3,401
  • 4
  • 33
  • 60