Java tutorial
//package com.java2s; /** * Copyright Microsoft Corporation, All Rights Reserved * * Licensed under MICROSOFT SOFTWARE LICENSE TERMS, * MICROSOFT RIGHTS MANAGEMENT SERVICE SDK UI LIBRARIES; * You may not use this file except in compliance with the License. * See the license for specific language governing permissions and limitations. * You may obtain a copy of the license (RMS SDK UI libraries - EULA.DOCX) at the * root directory of this project. * * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION * ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A * PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT. */ import android.content.Context; import android.content.Intent; import android.net.Uri; import android.text.Html; import android.text.SpannableStringBuilder; import android.text.TextPaint; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.text.style.URLSpan; import android.view.View; import android.widget.TextView; public class Main { /** * Make UI TextView a html link. * * @param context the context * @param textView the text view * @param html the html containing link info */ public static void makeTextViewAHTMLLink(final Context context, TextView textView, String html) { textView.setLinksClickable(true); textView.setMovementMethod(LinkMovementMethod.getInstance()); CharSequence sequence = Html.fromHtml(html); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(sequence); URLSpan[] urls = spannableStringBuilder.getSpans(0, sequence.length(), URLSpan.class); for (final URLSpan urlSpan : urls) { int start = spannableStringBuilder.getSpanStart(urlSpan); int end = spannableStringBuilder.getSpanEnd(urlSpan); int flags = spannableStringBuilder.getSpanFlags(urlSpan); ClickableSpan clickable = new ClickableSpan() { public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlSpan.getURL())); context.startActivity(intent); } @Override public void updateDrawState(TextPaint textPaint) { super.updateDrawState(textPaint); textPaint.setUnderlineText(false); } }; spannableStringBuilder.removeSpan(urlSpan); spannableStringBuilder.setSpan(clickable, start, end, flags); } textView.setText(spannableStringBuilder); } }