user45678 user45678 - 1 year ago 86
Android Question

How to identify #hashtag and "http://" link from a string in android

is it possible to find a #hashtag and an "http://" link from a string and color it ? I am using this in Android.

public void setTitle(String title) {
this.title = title;

Thanks in Advance

Answer Source

I have found an answer and here is the way to do it :

        SpannableString hashtagintitle = new SpannableString(imageAndTexts1.get(position).getTitle());
        Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashtagintitle);
        while (matcher.find())
            hashtagintitle.setSpan(new ForegroundColorSpan(Color.BLUE), matcher.start(), matcher.end(), 0);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download