Making a call from WebView with Intent is not working - java

I try to open the dialer while clicking on call button in post, but it is not working. All code is running but only this part not working.
I don't want to change the file uploading code, I only need to change call button code in webview:
public class MainActivity extends AppCompatActivity {
WebView webView;
private static final String TEL_PREFIX = "tel:";
public String url ="https://www.negotiatar.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient());
webView.setInitialScale(0);
webView.setWebViewClient(new CustomWebViewClient());
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if(url.startsWith(TEL_PREFIX)) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
}
}

you have a big mistake in your code it seems you have nested class inside the method, are you able to do this like you have made?

Related

Android webview doesn't open api.whatsapp

I'm trying to open an api.whatsapp link from my webpage using the Android web view but I faced the error ERR_UNKNOWN_URL_SCHEME.
I have changed the shouldOverrideUrlLoading method several times trying to fix it but the intent never works. Have someone faced the same problem before?
This is the current version of my code:
public class MainActivity extends AppCompatActivity {
// public static final String TAG = MainActivity.class.getSimpleName();
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mywebView.getSettings();
mywebView.loadUrl("https://mypage");
webSettings.setJavaScriptEnabled(true);
mywebView.setWebViewClient(new WebViewClient());
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//String url = request.getUrl().toString();
if (Uri.parse(url).getHost().equals("https://mypage")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Any suggestions?
Add Validating to your shouldOverrideUrlLoading method like:
Patterns.WEB_URL.matcher(url).matches();

Webview loading url but opening content in default browser?

It opens my link in Webview but when I try to open hyperlinks and content of the web or navigation link, it starts opening with the default browser.
public class MainActivity extends Activity {
private WebView mWebView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://");
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Objects.requireNonNull(Uri.parse(url).getHost()).endsWith("http://www.ieltsmadeeasy.tk/")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
I want to browse the whole website inside the web view, not any browser please help me
Here is some documentation about WebViews: https://developer.android.com/guide/webapps/webview#java
The problem is, that you don't override the shouldOverrideUrlLoading(view, url) function of the WebViewClient, as you use the default one.
If you use this as the WebViewClient, it should work (copied from examples in documentation):
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("https://www.example.com")) {
// This is my website, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
So, just take this class and change the line mWebView.setWebViewClient(new WebViewClient()); to mWebView.setWebViewClient(new MyWebViewClient());

Open new tab in same page with Android WebView

I am making an app for WebView with a header progress bar. It is working nicely but after loading the web page, if I click any link on the webpage, the app closes and opens the default browser. But instead, I want to stay in the WebView page after the click and open the new tab on the same page.
How can I do this?
public class MainActivity extends Activity {
private WebView webView;
private EditText urlEditText;
private ProgressBar progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// urlEditText = (EditText) findViewById(R.id.urlField);
webView = (WebView) findViewById(R.id.webView);
//webView = new WebView(this);
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);
webView.setWebChromeClient(new MyWebViewClient());
//Button openUrl = (Button) findViewById(R.id.goButton);
String url = "http://amrtube.com/result/index.php";
if (validateUrl(url)) {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptEnabled(true); // enable javascript
this.webView.getSettings().setAllowFileAccess(true);
this.webView.getSettings().setBuiltInZoomControls(true);
//this.mWebview.getSettings().setSupportZoom(true);
final Activity activity = this;
this.webView.setScrollBarStyle(33554432);
this.webView.setScrollbarFadingEnabled(false);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadUrl(url);
MainActivity.this.progress.setProgress(0);
}
}
private boolean validateUrl(String url) {
return true;
}
private class MyWebViewClient extends WebChromeClient {
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.toString());
return true;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
}
Replace your line with this
this.mWebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);

Starting intent in an extra WebView class instead in an inner class?

I wanted to keep my MainActivity small and clear. So I have created an seperate class for WebView instead of an inner class this way:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
mWebView.setWebViewClient(new CustomWebViewClient());
But now I wanted to start a new activity in CustomWebViewClient with the openInAppBrowser method:
public class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(PROJECT_REMOTE)) {
return false;
} else {
openInAppBrowser(this, url);
}
return true;
}
private void openInAppBrowser(Activity activity, String url) {
Intent intent = new Intent(activity, InAppBrowser.class);
intent.putExtra("url", url);
activity.startActivity(intent);
}
Since this and MainActivity.this won't work here, how could I reference the MainActivity to make this method work? Or is the inner class inside the MainActivity the better or only option?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
mWebView.setWebViewClient(new CustomWebViewClient(this));
public class CustomWebViewClient extends WebViewClient {
Activity activity;
public CustomWebViewClient (Activity activity) {
this.activity= activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(PROJECT_REMOTE)) {
return false;
} else {
openInAppBrowser(url);
}
return true;
}
private void openInAppBrowser(String url) {
Intent intent = new Intent(activity, InAppBrowser.class);
intent.putExtra("url", url);
activity.startActivity(intent);
}
As requested. I am not sure this will work, that's why I did not put on answer in the first place.

How to access to Webview declared in onCreate from outside?

I'm trying to develop some simple app based on WebView, and make possible to exchange data between android app and remote page.
Problem description:
loadUrl works within onCreate, but not working from outside of onCreate. Please see code below and explain what I doing wrong, why it's not working within WebAppInterface class:
public class MainActivity extends Activity {
public WebView view1;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "htxx://mydomain.com/";
final WebView view1 =(WebView) this.findViewById(R.id.webView);
view1.addJavascriptInterface(new WebAppInterface(this), "Android"); //exchange between web-js and webview
WebSettings webSettings = view1.getSettings();
webSettings.setJavaScriptEnabled(true);
view1.loadUrl(url);
view1.setWebViewClient(new com.example.project.MyAppWebViewClient() {
#Override
public void onPageFinished(WebView view1, String url) {
findViewById(R.id.progressBar1).setVisibility(View.GONE);
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
});
view1.setInitialScale(100);
///// ---> That's works:
view1.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
view1.loadUrl("javascript:somescript('Page is loaded')");
}
});
///// ---> That's works too:
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String device_id = tm.getDeviceId();
view1.loadUrl("javascript:somescript('"+device_id+"')");
}
});
}
public class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
#JavascriptInterface
public void showToast(String toast) {
//show toast from web-app (js to android)
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
#JavascriptInterface
public void printFromWeb() {
//show toast from web-app (js to android)
///// ---> That's NOT working :(
view1.loadUrl("javascript:somescript('Checking loadurl from outside of onCreate')");
}
}
}
Interesting, that not any errors occurs in Android studio.
You have public WebView view1; as a field and completely different final view1 inside onCreate(), so it's actually null in your public void printFromWeb() method.
Here is the safe route to go:
To access WebView "from outside", add a private field of WebView class:
private WebView webView;
In your onCreate() method, instantiate it:
webView = (WebView) findViewById(R.id.webView);
Add a getter method to access the instance:
public WebView getWebView() {
return webView;
}
And remember to only touch it only on UI thread.
Update: how to safely access views from not-ui thread:
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run(){
view1.loadUrl(...)
}
})

Categories

Resources