Thursday 20 February 2014

Linkify text

Hey Guys,

Lets see the use of Linkify in Android.

The Linkify class is available in Android SDK. With this class we can specify a regular expression to match, and a scheme to append before linkified text. The matched text is added after scheme to allow the correct data to be looked up.

For example, we want to linkify all text starts with either number or small case alphabet or capital case alphabet (with no spaces). Linkify can turn this in URI - something like http://www.google.com/text which can then be used to locate the correct application with data.

Here is the code snippet:

               TextView txtOutput = (TextView) findViewById(R.id.adb_output);
txtOutput .setText(" This is simple text To search");
Pattern wordMatcher = Pattern.compile("\\b[A-Za-z0-9]+\\b");   //you can change the regular expression as per your requirement.

String viewURL=    "http://www.google.com/";
Linkify.addLinks(txtOutput , wordMatcher , viewURL);


Please let me know if have any query.


No comments:

Post a Comment

Please share your thoughts