I am doing chat function, my company asked to detect chat content whether containing the URL? Then I can highlight the url and click to jump to the url web site!Can you help me? Thank you very much.
Asked
Active
Viewed 1,458 times
-3
-
Use the regex public static final String `URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";` – S M Jun 13 '16 at 04:18
-
Please check the following answer http://stackoverflow.com/questions/6910703/android-active-link-of-url-in-textview – Abdul Momen Khan Jun 13 '16 at 04:18
2 Answers
2
You can using your own regex or simply using android.Text.util.Linkify class like this:
TextView textContent;
String content;
textContent.setText(content);
Linkify.addLinks(textContent, Linkify.WEB_URLS);
if you want to make it automatically check phone number, email, etc., you can use Linkify.ALL on the second paramter.

ikhsanudinhakim
- 1,554
- 16
- 23
-
Thank you for your answer. Your method is automatic detection of the Android system. Can you point out the specific method to detect ? Or how the Android system is detected.I am using IOS.Even if the language is different, but I believe that the method process is certainly similar. – flynn Jun 13 '16 at 05:56
-
Oh, i suspect you are on android because of the android tag. If you would implement that on another platform you can simply do by implement your own URL regex, or check on android.Text.util.Linkify class on Android how it perform URL matcher. – ikhsanudinhakim Jun 13 '16 at 06:05
1
By using Regular expressions you can find string contains url or not. use this following code.
public static final String URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";
Pattern pattern = Pattern.compile(URL_REGEX);
Matcher matcher = pattern.matcher("google.com");//replace with string to compare
if(matcher.find()) {
Log.i("String contains URL");
}

RajaReddy PolamReddy
- 22,428
- 19
- 115
- 166
-
Thank you for your answer. But your answer can only be detected in a certain web site, and can not detect a string containing all the urls. And the position and length of the string can not be found and marked for clicking. – flynn Jun 13 '16 at 05:50