8

I have made a web-view of a website. Now I want to share some data from my web-view to whatsapp app. I was able to open whatsapp web, but my client wants me to open whatsapp application instead of whatsapp web. How can I do that?

This is what I have done to open whatsapp web in my site:

<a class="social-icon whatsapp" 
  href="javascript:popWin('https://api.whatsapp.com/send?text=Product%20Name:-<?php echo $productName; ?>.%20Product%20link:-<?php echo $productUrl; ?>', 'whatsapp', 'width=640, height=480, left=0, top=0, location=no, status=yes, scrollbars=yes, resizable=yes');"
  title="<?php echo $this->__('Share on Whatsapp') ?>" 
  data-action="share/whatsapp/share">
  <span><i class="fa fa-whatsapp"></i></span>
</a>
barbsan
  • 3,418
  • 11
  • 21
  • 28
Abhishek Kalotra
  • 139
  • 1
  • 3
  • 12

6 Answers6

13
@Override
        public boolean shouldOverrideUrlLoading(WebView wv, String url) {
            if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;
            }
            return false;
        }

Put this code in your mWebView.setWebViewClient(new WebViewClient(). it will be working perfectly for all link like tel:, whatsapp: etc.

  • 1
    Thats work fine!! myWebView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView wv, String url) { if(url.startsWith("tel:") || url.startsWith("whatsapp:")) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); startActivity(intent); return true; } return false; } }); – perezmirabile Jun 13 '20 at 15:18
  • Working like a charm TQVM ! – Ashraf Amin Dec 02 '21 at 08:18
7
   webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView wv, String url) {
            if(url.startsWith("tel:") || url.startsWith("whatsapp:") || url.startsWith("intent://") || url.startsWith("http://") ) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                webView.goBack();
                return true;
            }
            return false;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

            invalidateOptionsMenu();
        }

        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            try {
                invalidateOptionsMenu();
            } catch (Exception e) {

            }

            if (webView.canGoBack()) {
                webView.goBack();
            }

        }


        public void onPageFinished(WebView view, String url) {
            //     pullToRefresh.setRefreshing(false);

            invalidateOptionsMenu();
        }



    });

my problem solves using this code.

mahedi hassan
  • 141
  • 1
  • 4
5

Use this and works ok. (@NSMedia-Solution) Just put

'myWebView.goBack();'

before return (For not navigate in whatsApp web)

myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView wv, String url) {
                if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                    myWebView.goBack();
                    return true;
                }
                return false;
            }
        });
perezmirabile
  • 171
  • 2
  • 5
4

You can use, its working for me:

Option First-

<a href="whatsapp://send?text=Hello friend!" data-action="share/whatsapp/share">Share</a>

Option Second-

<a href="https://api.whatsapp.com/send?text=Hello friend!" data-action="share/whatsapp/share">Share</a> 

Both options are working for me..

If not working to your side then please update android version of your mobile phone & also update whatsapp version

Rakesh P
  • 438
  • 5
  • 19
2

Here I tried and it worked:

<a href="intent://send?text=Hello#Intent;scheme=whatsapp;package=com.whatsapp;end" class="button">Go to Whatsapp/>

See Chrome's docs about this here.

Bulat
  • 720
  • 7
  • 15
Jolson Da Costa
  • 1,095
  • 1
  • 12
  • 31
0

New version with updated shouldOverrideUrlLoading

@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    String url = request.getUrl().toString();
    if (url.endsWith(".pdf")) {
        view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        return true;
    }else{
        return false;
    }
}
Gianluca Demarinis
  • 1,964
  • 2
  • 15
  • 21