Suppose in out text/paragraph of textview, some words are clickable or hyperlinked. In case you have a task to open those hyperlinked text in a webview of any browser you have to use SpannableStringBuilder. Below code with details will help you to open those specific hyperlinked words which are appearing on textview.
// storing data into a string object
String text=pagesPostDetail.getPOST().getPost_detail();
// reading html text if any
CharSequence sequence = Html.fromHtml(text);
// creating object of SpannableStringBuilder class
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
UnderlineSpan[] underlines = strBuilder.getSpans(0, 10, UnderlineSpan.class);
// now searching underlines text from start to end
for(UnderlineSpan span : underlines) {
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan myActivityLauncher = new ClickableSpan() {
public void onClick(View view) {
// ACTION_VIEW to open intent webview chooser
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(text));
context.startActivity(intent);
}
};
strBuilder.setSpan(myActivityLauncher, start, end, flags);
}
// finally attaching to the textview
post_detail_Textview.setText(strBuilder);
post_detail_Textview.setLinksClickable(true);
post_detail_Textview.setMovementMethod(LinkMovementMethod.getInstance());
0 Comment(s)