I used this for Stripe Checkout which opens a new window in mobile devices for payments.
Based on @Shweta's response :
In your activity:
package myapp.app;
/*** imports ***/
public class LoggedActivity extends FragmentActivity
{
public WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.logged);
// retrieve the main container
LinearLayout container = (LinearLayout) findViewById(R.id.logged_webviews_container);
// layout params applied to the webviews in order to fit 100% the parent container
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
myWebView = new WebView(this);
myWebView.setLayoutParams(layoutParams);
myWebView.setWebViewClient(new BetterWebViewClient(this));
WebSettings settings = myWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setSupportMultipleWindows(true);
// on this instruction, we set our extended class below as the Parent Webview webchromeclient
myWebView.setWebChromeClient(new PopupWebView(this, myWebView, container, layoutParams));
// load URL
myWebView.loadUrl('http://www.mywebsite.com');
container.addView(myWebView);
}
}
Add this class which extends WebChromeClient
package myapp.app;
/*** imports ***/
public class PopupWebChromeClient extends WebChromeClient {
protected Activity activity;
protected WebView parentWebView;
protected RelativeLayout container;
protected WebView popupView;
PopupWebChromeClient(
Activity activity,
WebView parentWebView,
RelativeLayout container
)
{
super();
this.activity = activity;
this.parentWebView = parentWebView;
this.container = container;
}
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
this.parentWebView.setVisibility(WebView.GONE);
this.popupView = new WebView(this.activity);
// setup popuview and add
this.popupView.getSettings().setJavaScriptEnabled(true);
this.popupView.setWebChromeClient(this);
this.popupView.setWebViewClient(new ApkfWebViewClient(this.activity, true));
this.popupView.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT
));
this.container.addView(this.popupView);
// send popup window infos back to main (cross-document messaging)
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(popupView);
resultMsg.sendToTarget();
return true;
}
// remove new added webview on close
@Override
public void onCloseWindow(WebView window) {
this.popupView.setVisibility(WebView.GONE);
this.parentWebView.setVisibility(WebView.VISIBLE);
}
}
In your layout xml, don't set webviews since we create them on the fly.