Season
Season

Reputation: 1218

Android: TextView with url end with -/\n Linkify wrongly

Currently, I have a String like

http://www.example.com/defg-/\nletters


I put this String into a TextView, and make the url clickable by setAutoLinkMask(Linkify.WEB_URLS) and setMovementMethod(LinkMovementMethod.getInstance())

However, the link is recognize wrongly, where only

http://www.example.com/defg   <--missing "-/"

is highlighted but not

http://www.example.com/defg-/  <--I want this

, and results in a wrong url.

What should I do such that the url can be recognized correctly?


The Sample Result (2nd link is wrongly recognized)

enter image description here

Code Implementation

    txtNorm = (TextView) findViewById(R.id.txtNorm);
    txtNorm.setText("http://www.example.com/defg-/");
    txtNorm.setAutoLinkMask(Linkify.WEB_URLS);
    txtNorm.setMovementMethod(LinkMovementMethod.getInstance());

    txtCustom = (TextView) findViewById(R.id.txtCustom);
    txtCustom.setText("http://www.example.com/defg-/\nletters");
    txtCustom.setAutoLinkMask(Linkify.WEB_URLS);
    txtCustom.setMovementMethod(LinkMovementMethod.getInstance());

Upvotes: 4

Views: 652

Answers (1)

Tanim reja
Tanim reja

Reputation: 2188

i found a way you can try this.. at first you need to know that if you add -/ at the end of url this is not common format of standard Web Url. so i made a custom pattern ..

String urlRegex="[://.a-zA-Z_-]+-/"; // carefully set your pattern.
        Pattern pattern = Pattern.compile(urlRegex);
        String url1="press http://www.example.com/defg-/\\ or on Android& to search it on google";
        text.setText(url1);

        Matcher matcher1=Pattern.compile(urlRegex).matcher(url1);

        while (matcher1.find()) {

            final String tag = matcher1.group(0);

            Linkify.addLinks(text, pattern, tag);
        }

        text.setMovementMethod(LinkMovementMethod.getInstance());

Upvotes: 2

Related Questions