androidlinkify

Android Linkify lower cases the URL


I have a URL with some Upper case letters like http://myserver.com/apps/DroidApp.apk

When I passed this url to Android Linkify, the resulting link's letter case changed to http://myserver.com/apps/droidapp.apk

TextView upgradeLink = (TextView) findViewById(R.id.upgradeNow);
upgradeLink.setText("Download now.");
Pattern pattern = Pattern.compile("Download");
String scheme = "http://myserver.com/apps/DroidApp.apk+"?action=";
Log.e(MY_DEBUG_TAG,"Schema URL "+scheme); // printed http://myserver.com/apps/DroidApp.apk?action=
Linkify.addLinks(upgradeLink, pattern, scheme);

How can I over come this?


Solution

  • Internally, Linkify is calling

    public static final boolean addLinks(Spannable s, Pattern p, String scheme, null, null)
    

    Check the code for that method:

    public static final boolean addLinks(Spannable s, Pattern p,
            String scheme, MatchFilter matchFilter,
            TransformFilter transformFilter) {
        boolean hasMatches = false;
        String prefix = (scheme == null) ? "" : scheme.toLowerCase(); // <-- here is the problem!
        Matcher m = p.matcher(s);
    
        while (m.find()) {
            int start = m.start();
            int end = m.end();
            boolean allowed = true;
    
            if (matchFilter != null) {
                allowed = matchFilter.acceptMatch(s, start, end);
            }
    
            if (allowed) {
                String url = makeUrl(m.group(0), new String[] { prefix },
                                     m, transformFilter);
    
                applyLink(url, start, end, s);
                hasMatches = true;
            }
        }
    
        return hasMatches;
    }
    

    Extend Linkify and override this method, removing the scheme.toLowerCase() bit.