I'm using a webview in my Android App.
I have 3 buttons 1 for a link to a website one to call a number and one email button.
At first calling my website button worked, http://www.somelink.com .
But my tel: link was not working. So I integrated some code that made my tel: button work.
The problem is that it has made my website or http: button not work?
The html button just does nothing when you click 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;
}
}
You have two shouldOverrideUrlLoading
methods in your code. The only first one is actually called by a WebView
. Move code from 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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With