How to handle webview popup in Android?
I am trying to implement a paytab payment in android that uses a popup for further variation, but I am unable to handle the popup in android web browser.
For testing, I created two simple html files for showing and closing popups
window.html
<html>
<head>
<script language="Javascript">
function showFBWindow(){
url = "allowfbchild.html"
newwindow=window.open(url,'name','height=200,width=150');
if (window.focus) {newwindow.focus()}
}
</script>
</head>
<body>
<input type="button" OnClick="showFBWindow()" value="Open FB" />
</body>
</html>
allowfbchild.html
<html>
<head>
<script language="Javascript">
function redirectToFB(){
// window.opener.location.href="http://192.168.1.7/window.html";
self.close();
}
</script>
<h1>hello</h1>
</head>
<body>
<h1>hello</h1>
<input type="button" value="Ok" OnClick="redirectToFB()" />
<a href="www.google.com">google</a>
</body>
</html>
I tried this in my activity on Android: (Suggest if it will be done for other ways)
public class MainActivity extends Activity
{
RelativeLayout main;
// @Override
// public void onBackPressed()
// {
// if (webView.canGoBack())
// webView.goBack();
// else
// super.onBackPressed();
// }
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
try
{
setContentView(R.layout.activity_main);
main = (RelativeLayout) findViewById(R.id.main);
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new MyWebChromeclient());
main.addView(webView);
webView.loadUrl("http://192.168.1.7/window.html");
}
catch (Exception e)
{
e.printStackTrace();
}
}
class MyWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
System.err.println("load url" + url);
view.loadUrl(url);
return false;
}
@Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
setProgressBarIndeterminateVisibility(false);
System.err.println("finish url" + url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
setProgressBarIndeterminateVisibility(true);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
super.onReceivedError(view, errorCode, description, failingUrl);
System.err.println("error:" + description);
}
}
private class MyWebChromeclient extends WebChromeClient
{
@SuppressLint("SetJavaScriptEnabled")
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg)
{
System.err.println("create new");
WebView newWebView = new WebView(MainActivity.this);
newWebView.getSettings().setJavaScriptEnabled(true);
newWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
main.addView(newWebView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
newWebView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return false;
}
});
return true;
}
@Override
public void onCloseWindow(WebView window)
{
super.onCloseWindow(window);
System.err.println("closing");
// main.removeViewAt(main.getChildCount() - 1);
}
}
}
Please suggest a solution for this. Thanx.
+3
source to share
No one has answered this question yet
Check out similar questions: