Telephone link in webview is opening in browser - android

I have a telephone link on my webview app: tel:062123658 but when I click on it I get webpage not found.
This is my code:
public class FullscreenActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.loadUrl("http://www.mywebsite.nl");
webView.setVerticalScrollBarEnabled(false);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
How can I fix this?

You should override a function in WebViewClient:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.loadData("Hello World, 317.456.2564 ", "text/html","utf-8");
//webView.loadUrl("http://www.techjini.com/contactus.html");
webView.setVerticalScrollBarEnabled(false);
}
class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
}

Try this way:
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
});

Related

How to open external player when using WebView?

How to open external video player when using WebView on JS site? I try to open an external video player through the android application on the webview, but nothing works.
file MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
webView.loadUrl("https://google.com/");
WebViewClient webViewClient = new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
};
webView.setWebViewClient(webViewClient);
}
};
I tried using this code but nothing comes out
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(".avi") || url.contains(".ts") || url.contains(".mkv") || url.contains(".mp4")){
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri videoUri = Uri.parse(url);
intent.setDataAndType( videoUri, "application/x-mpegURL" );
startActivity( intent );
return true;
}else {
return false;
}
}

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.
After 1 second loading time :
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();
}
}
}
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;
}
});
}

How to open new screen on webview url click?

In my app i am using webview to display some web data, i want to implement a functionality where if a user clicks on any link a new screen (activity) will open and url's data will be displayed in that activity's webview. i have tried setting a webview client to webview and overriding its onPageStarted,shouldOverrideUrlLoading methods but they all open the url within the same screen.
Here is my code
webview.loadData(html, "text/html; charset=utf-8", "UTF-8");
webview.setWebViewClient(webViewClient);
}
WebViewClient webViewClient = new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (url.startsWith("http://")) {
Intent intent = new Intent(FirstActivity.this, WebActivity.class);
intent.putExtra("url", url);
startActivity(intent);
}
}
#Override
public void onPageFinished(WebView view, String url) {
}
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
};
How this can be implemented? any help will be appreciated
Try this:
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http://")) {
Intent intent = new Intent(FirstActivity.this, WebActivity.class);
intent.putExtra("url", url);
startActivity(intent);
return true;
}
return false;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (url.startsWith("http://")) {
Intent intent = new Intent(FirstActivity.this, WebActivity.class);
intent.putExtra("url", url);
startActivity(intent);
return true;
}
return false;
}
Try this. But you first need to set a start website like google.com
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.setWebViewClient(new MyBrowser());
view.loadUrl("[YOUR_WEBSITE]"); //try js alert
view.setWebChromeClient(new WebChromeClient()); // adding js alert support
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mailto:") || url.startsWith("mms:") || url.startsWith("mmsto:") || url.startsWith("market:")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
else {
view.loadUrl(url);
return true;
}
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
view.goBack(); //method goback()
return true;
}
return super.onKeyDown(keyCode, event);
}
}
If links are external to the site, you could do something like this:
webview.setWebViewClient(new LinkExtWebViewClient());
And then:
public class LinkExtWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(" if your site is www.myurl.com, here you have to insert 'myurl.com' ")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
Let me know if it works :)

i have a webview app and facing that when i click on Hyperlink then it open in default browse

I have a webview app and facing a problem that
when I click on Hyperlink then it opens in default browser.
But I want to open that link in same webview.
How to open it in same webview.
Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("file:///android_asset/abc/index.html");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("file:///android_asset/abc/index.html")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Do it like,
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Return false in all cases.
ok.. you have added a WebViewClient also..
then try it in this way...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
this should work for you..

WebView shouldOverrideUrlLoading Wont Play Video

My app displays a webpage using a WebView. I want the user to be able to click on a link to a video, and the video be played in landscape mode. Where I am now, the video doesn't even play??
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mnwv_main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setPluginsEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.meanwhileinwv.com");
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
startActivity(in);
return true;
}
else
return false;
}
Change this:
...
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.meanwhileinwv.com");
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
startActivity(in);
return true;
}
else
return false;
}
with this:
...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
startActivity(in);
return true;
}
else
return false;
}
});
myWebView.loadUrl("http://www.meanwhileinwv.com");
}

Categories

Resources