Featured post

Marshmallow Features Point by Point

Android Runtime (“ART”) Improved application performance and lower memory overhead for faster  multi-tasking. Battery Doze...

Thursday 29 January 2015

Textview multiple underline text onClick dynamically in android


Follow below step
====================

Step 1:-

String str_links=
 "<h2 class='heading-open subheader'>Culture</h2><div class='bodyfold'><p>Throughout Cambodia's long history, religion has been a major source of cultural inspiration. Over nearly two millennia, Cambodians have developed a unique <a href='/section/Khmer_people'>Khmer</a> belief from the syncreticism of indigenous animistic beliefs and the Indian religions of Buddhism and Hinduism";  

// In onCreate()

TextView text_view = null;
text_view = (TextView) findViewById(R.id.textView1);

text_view.setLinksClickable(true);
text_view.setMovementMethod(LinkMovementMethod.getInstance());
setTextViewHTML(text_view, str_links );

// Two Function

protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span, final URLSpan[] urls,final int count)
{
final int start = strBuilder.getSpanStart(span);
final int end = strBuilder.getSpanEnd(span);
final int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
// Do something with span.getURL() to handle the link click...
Log.e("log start", start+"");
Log.e("log end", end+"");
Log.e("log flags", flags+"");
// Log.e("log count", count+"");
// Log.e("URLSpan", urls[count].getURL()+"");
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}

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);   
int count=0;
for(URLSpan span : urls) {
makeLinkClickable(strBuilder, span,urls,count);
Log.e("log count", count+"");
Log.e("URLSpan", urls[count]+"");

count++;
}
text.setText(strBuilder);       
}

1 comment:

  1. Hey...Dear.... its perfect solution on textview html content load on clickable particular values. Solved my problems which i need to it... :) Happy :)

    ReplyDelete