remove Underlines From Links in TextView - Android User Interface

Android examples for User Interface:TextView

Description

remove Underlines From Links in TextView

Demo Code


import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.URLSpan;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main{
    public static void removeUnderlinesFromLinks(@NonNull TextView textView) {
        CharSequence text = textView.getText();
        if (text instanceof Spanned) {
            Spannable spannable = new SpannableString(text);
            removeUnderlinesFromLinks(spannable, spannable.getSpans(0,
                    spannable.length(), URLSpan.class));
            textView.setText(spannable);
        }/*www .j  a va2 s  .c  o m*/
    }
    public static void removeUnderlinesFromLinks(
            @NonNull Spannable spannable, @NonNull URLSpan[] spans) {
        for (URLSpan span : spans) {
            int start = spannable.getSpanStart(span);
            int end = spannable.getSpanEnd(span);
            spannable.removeSpan(span);
            span = new URLSpanNoUnderline(span.getURL());
            spannable.setSpan(span, start, end, 0);
        }
    }
    public static Object[] getSpans(@NonNull Spanned spanned, int start,
            int end) {
        Class<Object> anyType = Object.class;
        return spanned.getSpans(start, end, anyType);
    }
}

Related Tutorials