How to Enable Android Webview use New Window and Support Auth0 login
Android webview can support multiple browse windows, but it needs to be configured. Usually it can be done with the OncreateWindow method.
The multiple window feature is important if your webview needs open another window or wants use google,Facebook etc Logins, called Auth0. If webview doesn’t support multiple windows, these logins can fail and window invoking links may not respond (but this unresponsive links can be fixed by disabling the multiple window support).
So here is an example for letting the webview open multiple window easily, this code should be placed under the WebChromeClient method.
First, add these options in your activity’s main webview, adding this will allow webview to use multiple windows:
WebSettings webSettings = webview.getSettings(); webSettings.setJavaScriptEnabled(true); webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setSupportMultipleWindows(true);
Secondly, add these above Oncreate method in your webview Activity:
private WebView mWebviewPop; private AlertDialog dialog; private AlertDialog builder; private static final String uanormal = "Mozilla/5.0 (Linux; U; Android 2.3.3; en-gb; " + "Nexus S Build/GRI20) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
Note: The string uanormal is used here and it is a custom useragent, because if it is not used, the google login will fail because of the disallowed useragent.
Next,in your webchrome client method, add this:
Webview.setWebChromeClient(new WebChromeClient() { @Override public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) { mWebviewPop = new WebView(getApplicationContext()); mWebviewPop.setWebChromeClient(new WebChromeClient()); mWebviewPop.getSettings().setJavaScriptEnabled(true); mWebviewPop.getSettings().setSavePassword(true); mWebviewPop.getSettings().setSaveFormData(true); mWebviewPop.getSettings().setUserAgentString(uanormal); mWebviewPop.setVerticalScrollBarEnabled(false); mWebviewPop.setHorizontalScrollBarEnabled(false); mWebviewPop.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false; try { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); view.getContext().startActivity(intent); return true; } catch (Exception e) { // Log.i(TAG, "shouldOverrideUrlLoading Exception:" + e); return true; } } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { dialog = new AlertDialog.Builder(Webactivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT).create(); dialog.setMessage("Please wait..."); dialog.setTitle("Loading.."); dialog.setIcon(R.drawable.yourimage); dialog.show(); } @Override public void onPageFinished(WebView view, String url) { // Page loading finished dialog.dismiss(); }}); builder = new AlertDialog.Builder(YourActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT).create(); builder.setView(mWebviewPop); builder.setButton("Close", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { mWebviewPop.destroy(); dialog.dismiss(); } }); builder.show(); builder.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { cookieManager.setAcceptThirdPartyCookies(mWebviewPop, true); } WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj; transport.setWebView(mWebviewPop); resultMsg.sendToTarget(); return true; } @Override public void onCloseWindow(WebView window) { //Toast.makeText(mContext,"onCloseWindow called",Toast.LENGTH_SHORT).show(); try { mWebviewPop.destroy(); } catch (Exception e) { } try { builder.dismiss(); } catch (Exception k) { } }
This is it, if you face any error, let me know
If you want me fix your webview file
Update: I see many people are having trouble to implement this or other webview codes in their Android project, this is because some codes are long and can be complex if you are new to development.
So if you really can’t put this code in your app to enable webview multiple window, i can do it for you for a small fee of $5
Just Email me your webview activity file and i will fix your webview errors. You can pay me using paypal or UPI
I will make sure your app supports webview multi windows, it is guaranteed. You can also send me emails to fix other errors of your webview. For more details, contact me at [email protected]
See a list of other services i offer in this page Services