0

I have created a simple website app using webview in android studio and given it internet access permission also enabled java script on app

When I have added a WhatsApp share button using this code

 <button>
<a href="whatsapp://send?text=HERE GOES THE URL ENCODED TEXT YOU WANT TO SHARE" data-action="share/whatsapp/share">Share via Whatsapp</a>
</button>

This is working well in all mobile browsers but when I try to use this on my app I am getting an error screen saying

Web page not available

The web page at whatsapp://send?text=I'm%20interested%20in%20your%20car%20for%20salecould not be loaded because:

net::ERR_UNKNOWN_URL_SCHEME

This is my code in mainactivity.java

package com.a4extras.ukzz.whatsappdaily;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;




public class MainActivity extends AppCompatActivity {

    private WebView mywebView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebView = (WebView)findViewById(R.id.WebView);
        WebSettings webSettings = mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebView.loadUrl("http://ukzz4u.blogspot.com/2017/11/whatsapp-share-01.html");
        mywebView.setWebViewClient(new WebViewClient());
    }



    @Override
    public void onBackPressed() {
        if (mywebView.canGoBack()) {
            mywebView.goBack();
        } else {
            super.onBackPressed();
        }
    }}

This is because my app is taking whatsapp:// as a URL So how can prevent this so that the Share button will work properly

Please help me with necessary script Thanks

Alberto M
  • 1,608
  • 1
  • 18
  • 42
low IQ
  • 1
  • 1
  • 2

2 Answers2

2

You should override url loading for that particular url in your WebViewClient.

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        boolean overrideUrlLoading = false;

        if (url != null && url.startsWith("whatsapp://")) {

            view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

            overrideUrlLoading = true;

        } else {

            view.loadUrl(url);
        }

        return overrideUrlLoading;
    }
pvrforpranavvr
  • 2,708
  • 2
  • 24
  • 34
0

Using the following code i am able to open the url in webview: Put the blow code snippet to your onCreate() method

wv1.setWebViewClient(new MyBrowser());
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl("http://ukzz4u.blogspot.com/2017/11/whatsapp-share-01.html");

And then add this class to your activity:

private class MyBrowser extends WebViewClient {
         @Override
         public boolean shouldOverrideUrlLoading(WebView view, String url)  {
             view.loadUrl(url);
             return true;
         }
     }
Alberto M
  • 1,608
  • 1
  • 18
  • 42