linkify string and return SpannableString - Android android.text

Android examples for android.text:SpannableString

Description

linkify string and return SpannableString

Demo Code


//package com.java2s;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static SpannableString linkify(String text) {
        SpannableString spannableString = new SpannableString(text);
        Matcher matcher = Pattern.compile("[#@]([A-Za-z0-9_-]+)").matcher(
                spannableString);/*from   ww  w  .j a v a  2  s. c  o  m*/
        while (matcher.find())
            spannableString.setSpan(
                    new ForegroundColorSpan(Color.parseColor("teal")),
                    matcher.start(), matcher.end(), 0);

        return spannableString;
    }
}

Related Tutorials