Reputation: 41
We have a scenario where we are opening a URL in a webview in an android application .Issue is there are no pop-ups coming up like they come up in browser. Once the web page is loaded there is a button which when clicked gives popup coming at the bottom of the screen "URL wants to access location" with deny and allow buttons.This behavior works as mentioned in browser but when the same page is loaded inside a Webview there is no popup coming up and activity indicator just keeps spinning.
Saw in various blogs that this works with custom webchrome client. Implementation currently done is as below :-
Code in the activity for webview :-
onCreate -
webView = (WebView) findViewById(R.id.webView_main);
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptEnabled(true); // enable java script
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setBuiltInZoomControls(true);
webViewSettings.setLoadWithOverviewMode(true);
webViewSettings.setUseWideViewPort(true);
webView.setWebChromeClient(new CustomWebChromeClient());
final String link = "URL to be accessed";
webView.loadUrl(link);
Implementation for custom webchrome client is :-
final class CustomWebChromeClient extends WebChromeClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
if (progressbar.isShowing()) {
} else {
progressbar.show();
}
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " + url);
if (progressbar.isShowing()) {
progressbar.dismiss();
}
if (webView.canGoBack()) {
Log.i(TAG, "button enabled");
// btnBack.setVisibility(View.VISIBLE);
// itemBack.setVisible(true);
} else {
Log.i(TAG, "button disbaled");
// btnBack.setVisibility(View.INVISIBLE);
// itemBack.setVisible(false);
}
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(DetailActivity.this, description,
Toast.LENGTH_SHORT).show();
if (progressbar.isShowing()) {
progressbar.dismiss();
}
if (webView.canGoBack()) {
Log.i(TAG, "button enabled");
// btnBack.setVisibility(View.VISIBLE);
// itemBack.setVisible(true);
} else {
Log.i(TAG, "button disbaled");
// btnBack.setVisibility(View.INVISIBLE);
// itemBack.setVisible(false);
}
}
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
super.onJsAlert(view, url, message, result);
final JsResult finalRes = result;
new AlertDialog.Builder(view.getContext())
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finalRes.confirm();
}
})
.setCancelable(false)
.create()
.show();
return true;
}
@Override
public boolean onJsConfirm(WebView view, String url, String message,
final JsResult result) {
new AlertDialog.Builder(mContext)
.setTitle("App Titler")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.cancel();
}
}).create().show();
return true;
}
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, JsPromptResult result) {
// TODO Auto-generated method stub
System.out.println("JS prompt called");
return super.onJsPrompt(view, url, message, defaultValue, result);
}
}
Is there any way we can have the same behavior in the webview like in browser ? Is this related to android sdk version and if so is there any workaround ?
P.S :- Using android SDK 4.4.2 ( API level - 19 )
Upvotes: 2
Views: 3073
Reputation: 41
We were able to get the location related popup in webview using below code :-
webView.setWebChromeClient(new WebChromeClient()
{
public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
Upvotes: 2