4

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());
Season
  • 1,178
  • 2
  • 22
  • 42

1 Answers1

2

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());
Tanim reja
  • 2,120
  • 1
  • 16
  • 23
  • sorry, but how could you extract the proper link? My problem is exactly asking for how to extract the link for larfe text. – Season Jan 02 '16 at 08:19
  • 1
    thankyou for answering, maybe I will use the URL pattern provided in code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149 – Season Jan 02 '16 at 09:43