4

I have three regular expression:

Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)");
Pattern hashtagPattern = Pattern.compile("(#[A-Za-z0-9_-]+)");
Pattern urlPattern = Patterns.WEB_URL;

I have a string :

This is a #sample #twitter text of @tom_cruise with a link http://tom_cruise.me

I need to match this text with the above three regular expression and color the matched text with Blue and set the final text in a TextView. How can I achieve that?

It is to be mentioned that I don't need to Linkify the text, only coloring. And I am not using Twitter4j Library.

Kaidul
  • 15,409
  • 15
  • 81
  • 150

2 Answers2

7

I replaced http://tom_cruise.me with http://www.google.com. Try the following:

String a = "This is a #sample #twitter text of @tom_cruise with a link http://www.google.com";

Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)");
Pattern hashtagPattern = Pattern.compile("(#[A-Za-z0-9_-]+)");
Pattern urlPattern = Patterns.WEB_URL;

StringBuffer sb = new StringBuffer(a.length());
Matcher o = hashtagPattern.matcher(a);

while (o.find()) {
    o.appendReplacement(sb, "<font color=\"#437C17\">" + o.group(1) + "</font>");
}
o.appendTail(sb);

Matcher n = mentionPattern.matcher(sb.toString());
sb = new StringBuffer(sb.length());

while (n.find()) {
    n.appendReplacement(sb, "<font color=\"#657383\">" + n.group(1) + "</font>");
}
n.appendTail(sb);

Matcher m = urlPattern.matcher(sb.toString());
sb = new StringBuffer(sb.length());

while (m.find()) {
    m.appendReplacement(sb, "<font color=\"#EDDA74\">" + m.group(1) + "</font>");
}
m.appendTail(sb);

textView.setText(Html.fromHtml(sb.toString()));
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Thanks for answer :) Everything seems okay except two things. How can I put color code instead of blue? I tried it but not working. And the '@' and '#' are not showing in the final text – Kaidul Jul 25 '13 at 05:17
  • @typedef The required portions of the text aren't blue? Or are you saying that you are not able to use a different color code. – Vikram Jul 25 '13 at 05:24
  • I need a different color code close to blue like `#6ccff6` but after applying `` The color isn't change. Instead it is showing the markup in textview – Kaidul Jul 25 '13 at 05:27
  • @typedef No, the codes aren't working as expected. Let me try and figure out a workaround. – Vikram Jul 25 '13 at 05:31
  • Please edit your answer because my regular expression was little bit buggy. it should be `(@[A-Za-z0-9_-]+)` instead of `@([A-Za-z0-9_-]+)` and `(#[A-Za-z0-9_-]+)` instead of `#([A-Za-z0-9_-]+)` and then no need to prepend `@` and `#` first. Please let me know if you can fix the color code issue – Kaidul Jul 25 '13 at 05:36
  • Also `` solve my problem. Please edit your answer with this. Thanks a lot! :) – Kaidul Jul 25 '13 at 05:44
  • 1
    @typedef I have edited my answer above after solving the color code problem in a hackish way. What we are doing here is passing the `StringBuffer` from one `Matcher` to the next. So, the `Matcher` with `(#[A-Za-z0-9_-]+)` is messing up with color codes. In my edited code above, I am passing the original string to `Pattern hashtagPattern` first. And then, whatever comes out is passed to the other matchers. This solves your color code problem. And, thanks for the accept! – Vikram Jul 25 '13 at 05:50
  • Now your code is perfectly working! Thanks a lot! You've saved my day! – Kaidul Jul 25 '13 at 05:59
  • 1
    Great code, also check this for making clickable with SpannableString http://stackoverflow.com/a/7837610/3736955 – Jemshit Jan 05 '16 at 08:40
0

Take a look at SpannableString and SpannableStringBuilder. An example of using the SpannableStringBuilder is available at https://stackoverflow.com/a/16061128/1321873

You could write a method that accepts the non-styled String and returns a CharSequence like so:

private CharSequence getStyledTweet(String tweet){
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder(tweet);
    //Find the indices of the hashtag pattern, mention pattern and url patterns 
    //and set the spans accordingly
    //...
    return stringBuilder;
}

and then use the return value from the above to set the text of the TextView

TextView tView = (TextView)findViewById(R.id.myText);
tView.setText(getStyledTweet(tweet));
Community
  • 1
  • 1
Rajesh
  • 15,724
  • 7
  • 46
  • 95