android - linkyfy some text in TextView -
i have string in textview , want linkify substring string. example:
click here know more.
i'm getting string dynamically. have search if has click here , convert link .how can linkify "click here".
to find pattern inside text , replace it, use this:
pattern p = pattern.compile("click here"); matcher m = p.matcher("for more info, click here"); stringbuffer sb = new stringbuffer(); boolean result = m.find(); while(result) { m.appendreplacement(sb, "<a href=\"www.mywebsite.com\">click here</a>"); result = m.find(); } m.appendtail(sb); string strwithlink = sb.tostring(); yourtextview.settext(html.fromhtml(strwithlink)); yourtextview.setmovementmethod(linkmovementmethod.getinstance()) this code search inside string , replaces "click here" link.
and @ end, not add android:autolink="web" xml resource (section textview), otherwise a-tags not rendered correctly , not clickable longer.
Comments
Post a Comment