I'm trying to add ClickableSpan to some dynamic pdf urls inside my dialog window. Why I want to do this is because currently, pdf files are not opening in the default device browser. However, some devices with pdf viewer app already installed can download the pdf. But I need to open the pdf in all devices.

What Im trying to do here is set a clickablespan to those pdf links and open in a webview.

My issue is the onClick method seems not getting called and still, the link is trying to open in the default browser.

I have tried making

setLinksClickable(false);

and commenting out the

messageText.setMovementMethod(LinkMovementMethod.getInstance());

but still, the onClick method is not get executed. but interestingly, updateDrawState(TextPaint ds) method is getting executed and I can change link color as I want.

This message contains other URLs, email addresses and phone numbers which, I need to be clickable while making only pdf URLs to be handled by the clickableSpan .

Appreciate if someone can help me to figure out the issue here or another way to achieve this.

Following is the code

Thanks in advance

 TextView messageText = customeDialog.findViewById(R.id.text);
 final WebView webView = customeDialog.findViewById(R.id.pdf_viewer);

 Spanned formattedString  = Utils.getHtmlFromString(message);
 messageText.setText(formattedString);

 Map<String, String> links = Utils.extractUrls(URLDecoder.decode(message));
    if(null != links && links.size() > 0 ){

        Iterator it = links.entrySet().iterator();
        while (it.hasNext()) {
            final Map.Entry pair = (Map.Entry)it.next();
            if(pair.getKey().toString().contains(".pdf")) {

                System.out.println("////PDF display name : " + pair.getKey());
                System.out.println("////PDF URL : " + pair.getValue());
                ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View textView) {
                        webView.getSettings().setSupportZoom(true);
                        webView.getSettings().setJavaScriptEnabled(true);
                        webView.loadUrl(pair.getValue().toString());
                    }

                    public void updateDrawState(TextPaint ds) {// override updateDrawState
                        ds.setUnderlineText(true); // set to false to remove underline
                        ds.setColor(Color.RED);
                    }
                };

                int startingPosition = formattedString.toString().indexOf(pair.getKey().toString());
                int endingPosition = formattedString.toString().indexOf(pair.getKey().toString()) + pair.getKey().toString().length();


                spannableString.setSpan(clickableSpan, startingPosition, endingPosition, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

        }

        messageText.setText(spannableString);
    }
    messageText.setLinksClickable(true);
    messageText.setMovementMethod(LinkMovementMethod.getInstance());

Related posts

Recent Viewed