Ad

How To Open Dialerapp In Android From Webview

- 1 answer

I have this code in my mainactivity.java file:

package com.business.i13bubbles;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;

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.view2);
    WebSettings webSettings = MywebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    MywebView.loadUrl("http://www.13bubbles.com");
    MywebView.setWebViewClient(new WebViewClient());
}

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

   public boolean shouldOverrideUrlLoading(WebView view2, String url) {
    if (url.startsWith("tel:")) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:123"));
        startActivity(intent);
    } else if (url.startsWith("http:") || url.startsWith("https:")) {
        view2.loadUrl(url); }
    return true;
}
}

I get no errors here. There is a link on the site:

<a target="_blank" rel="nofollow noreferrer" href="tel:+919910673876" class="btn btn-border-filled">Call @ 9910 6738 76</a>

It doesn't open the dialer app. Am I doing something wrong ??? I have been trying to fix this since morning but couldn't do anything.

Ad

Answer

Use this code in your shouldOverrideUrlLoading() method:

public boolean shouldOverrideUrlLoading(WebView view2, String url) {
    if (url.contains("tel:")) { 
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse(url))
        startActivity(intent);
        return true;
    }
}
Ad
source: stackoverflow.com
Ad