I am trying to insert links to TextView in order to make them start a new activity. I wrote a method to my Utils
class to do that:
public static final String tagPattern = "#([^ ]+)";
public static final String tagReplace = "<a href=\"org.openihs.seendroid://display/tag/$1/\">#$1</a>";
public static final String userPattern = "@([^ ]+)";
public static final String userReplace = "<a href=\"org.openihs.seendroid://display/user/$1/\">@$1</a>";
static public void linkify(TextView view) {
String text = view.getText().toString();
text = text.replaceAll(Utils.tagPattern, Utils.tagReplace);
text = text.replaceAll(Utils.userPattern, Utils.userReplace);
view.setMovementMethod(LinkMovementMethod.getInstance());
view.setText(Html.fromHtml(text));
Log.d("SeenDroid", text);
Linkify.addLinks(view, Linkify.ALL);
}
An example output (to the logcat) is:
foo <a href="org.openihs.seendroid://display/tag/bar/">#bar</a> baz http://google.fr/
And the real UI gives http://google.fr/ as a link (expected behavior), but #bar
as a normal text (unexpected behavior).
The TextView is in a ListView.
Any idea to solve that issue?
Regards, ProgVal
Don't do the replaces. Html.fromHtml
already handles the tags properly.
Linkify works a little different than the way you're trying to use it.
public static final String AUTHORITY = "your_package.your_content_provider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/tag/");
Pattern pattern = Pattern.compile(tagPattern);
Linkify.addLinks(view, pattern, CONTENT_URI.toString());
Where CONTENT_URI refers to a ContentProvider you have registered in your manifest.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.