Http: // href link does not work in Android

I am using webview in my android app.

I have 3 buttons 1 for a link to a website where you can call by number and email.

First, a button call to the website http://www.somelink.com .

But my tel: link did not work. So I included some code that made me work with the body:

The problem is that he launched my website or http button:

The html button does nothing when you click on it.

package de.sonae.novolam; import android.annotation.SuppressLint; import android.app.Fragment; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView; import android.webkit.WebViewClient; @SuppressLint("SetJavaScriptEnabled") public class DFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View mainView = (View) inflater.inflate(R.layout.dfragment, container, false); WebView webView = (WebView) mainView.findViewById(R.id.webview); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView wv, String url) { if (url.startsWith("tel:")) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); startActivity(intent); } return true; } }); webView.loadUrl("file:///android_asset/contact.html"); return mainView; } public boolean shouldOverrideUrlLoading(WebView webView, String url) { if( url.startsWith("http:") || url.startsWith("https:") ) { webView.loadUrl(url); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity( intent ); } else if (url.startsWith("mailto:")) { } // Otherwise allow the OS to handle it Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity( intent ); return true; } } 
+4
source share
3 answers

You need to specify a web view when you handle url loading yourself, and when you want to delegate processing on it. Just return true from shouldOverrideUrlLoading when you handle the loading of the URL with your action. Otherwise, returns false:

 webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView wv, String url) { if (url.startsWith("tel:")) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); startActivity(intent); return true; // we handled the url loading } return false; // let WebView handle this event } }); 
+3
source

There are two shouldOverrideUrlLoading methods in your code. Only the first one is actually called by a WebView . Move the code from the second shouldOverrideUrlLoading to the first and it will work.

 webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView wv, String url) { if (url.startsWith("tel:")) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); startActivity(intent); return true; } else if( url.startsWith("http:") || url.startsWith("https:") ) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); return true; } else if (url.startsWith("mailto:")) { // TODO : handle mail url return true; } return false; } }); 
+7
source

The shouldOverrideUrlLoading method should return true if you want to override (i.e. prevent default processing) or false if you want everything to be normal.

 @Override public boolean shouldOverrideUrlLoading(WebView wv, String url) { if (url.startsWith("tel:")) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); startActivity(intent); return true; } return false; } 
+1
source

All Articles