I’m trying to use the Android Html.fromHtml method to parse a text like a tweet with @ mentions in it. However, I have the username of the current user and I wish to highlight this to the user as being themselves.
I’m trying to with this code, which doesn’t work:
in = in.replace("@" + u.username, "@<font background=#0099ff color=#fff><a href='dailybooth://user/"+
u.username+"'>"+u.username+"</a></font>");
In theory, I think it should work, however it doesn’t color at all!
This turned out to be a doozy.
Neither the
fonttag nor inline styles on theatag worked for me. I had to resort to using Spannables directly:This will, for example, set the colors of the link in the way you wanted. You can look for other Spannables in android.text.style to achieve other effects.
Edit for replace you could do something like:
The -1 in
split()is what you would need to deal with matches coming at the end of strings, as per the split docs.