heisenberg3008
heisenberg3008

Reputation: 65

Whatsapp Api not getting loaded in webview android

I am able to load the website properly in the app but it has a function which redirects users to whatsapp. The api works fine on mobile browser and on PC/laptop. But in the android app it loads for a second and then says webpage unavailable. What am I missing?

Image 1 stays only for a second. enter image description here

After 1 second loading time : enter image description here

Main Java Code:

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);
        mywebView.loadUrl("https://zzzz/");
        WebSettings webSettings=mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebView.setWebViewClient(new WebViewClient());
    }
    @Override
    public void onBackPressed(){
        if(mywebView.canGoBack()) {
            mywebView.goBack();
        }
        else{
            super.onBackPressed();
        }
    }
}

Upvotes: 1

Views: 1621

Answers (1)

Quick learner
Quick learner

Reputation: 11477

Override this shouldOverrideUrlLoading and do this in it

Code

// Force links and redirects to open in the WebView instead of in a browser
    mWebView.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;
            }
    });

Your code should be like this

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);
        mywebView.loadUrl("https://naturesexpress.in/");
        WebSettings webSettings=mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.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;
                }
        });
    }

Upvotes: 1

Related Questions