Issue
I want to set html text in textview like this:
<a href="?id=1>Toast id1</a>hello there <a href="?id=2>Toast id2</a>
hello there <a href="?id=3> Toast id3</a>
I want to show the different toast after clicking on different link with different id(query string).
Solution
String htmlText = "<body><h1>Heading Text</h1><p>This tutorial "
+ "explains how to display "
+ "<strong>HTML </strong>text in android text view. </p>"
+ "Example from <a href=\"www.ushatek.com\">"
+ "Ushatek<a>,<a href=\"www.google.com\">"
+ "Google<a>,<a href=\"Male\">"
+ "Male<a>,<a href=\"Female\">"
+ "Female<a></body>";
setTextViewHTML(textView, htmlText);
protected void setTextViewHTML(TextView text, String html) {
CharSequence sequence = Html.fromHtml(html);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(),
URLSpan.class);
for (URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
text.setText(strBuilder);
}
protected void makeLinkClickable(SpannableStringBuilder strBuilder,
final URLSpan span) {
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
Toast.makeText(getApplicationContext(), span.getURL(), Toast.LENGTH_LONG).show();
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
Answered By - Android Boy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.