Philipp Redeker
Philipp Redeker

Reputation: 3878

Linkify adds Text to the End of Links

I would like to add a link to a textview and have the following code:

TextView textview = (TextView) findViewById(R.id.mytext);
Pattern myPattern = Pattern.compile("WordToBeLinked");
String link = "http://mydomain.com/something";
Linkify.addLinks(textview, myPattern, link);

So everything works as it should: the word "WordToBeLinked" is linked and it opens the browser with the link BUT somehow Linkify adds the "WordToBeLinked" to the URL so the URL that is called looks like this:

http://mydomain.com/somethingWordToBeLinked

Can somebody please tell me what i did wrong? Thanks

Upvotes: 5

Views: 930

Answers (2)

Snicolas
Snicolas

Reputation: 38168

That is the exact behavior described in the docs of linkify.

If you want to replace your word by a link then do :

you could use the overloaded method and use a transform filter to rewrite the final url.

Upvotes: 0

Gopal
Gopal

Reputation: 1719

You have to use TransformFilter. Hope this help.

            TextView textview = (TextView) findViewById(R.id.mytext);
            textview .setText("WordToBeLinked");

            TransformFilter mentionFilter = new TransformFilter() {
                public final String transformUrl(final Matcher match, String url) {
                    return new String("http://mydomain.com/something");
                }
            };

            Pattern pattern = Pattern.compile(".");
            String scheme = "";
            Linkify.addLinks(textview, pattern, scheme, null, mentionFilter);

Since there is no Pattern and scheme in your case, so they are just place holder.

Upvotes: 6

Related Questions