Search code examples
androidtextviewlinkify

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


Solution

  • 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.