Understanding Android’s WebChromeClient onCreateWindow Method

A complete demo application is on my GitHub account at:

Android’s WebViews are pretty easy to use with the notable exception of the WebChromeClient’s onCreateWindow. It is called when the webview would like the host application to create a new window. And new is the key. If you don’t create a new webview, the call to onCreateWindow will fail, and worse yet, it will fail without exceptions or errors.

I initially had trouble with onCreateWindow. I couldn’t seem to get it to work correctly and unfortunately I couldn’t find a good working example of its use anywhere on the Internet. So I very carefully read the documentation again and follow the instructions to the letter. My first attempt to use onCreateWindow didn’t create a new window, instead I tried to recycle a child webview that was already on the page. It didn’t work, it didn’t cause an error, and it did create an Intent for the web browser.

In order to get onCreateWindow to work, I created a special layout first. Take a look at main.xml. In it holds both the main webview and a relative layout, mainBrowserLayout, which will hold the child web view once it is created. 

The heart of this application is off course the onCreateWindow method. The first thing it does is remove any current child windows which may still be attached to browserLayout. Then it makes the entire childLayout visible. It is by default set invisible. Next we create a new webview and give it some settings to make it function correctly. I added the new web to browserLayout. Then I do all of the required work to inform the new webview that it will be used to display the new link.
Next we set the webview client. I only implement this method so that I have access to the onPageFinished method. I use this method to read the title of the view, then set that text in mainTitleText TextView. 

To inform the system that our newly create webview will be used for the link’s page, we convert the resultMsg.obj into a WebViewTransport, then we call the setWebView with a reference to the new webview. 

The last thing we do isn’t necessary but makes things look cooler. We animate the childLayout sliding up from the bottom of the screen. The animation for it and its counterpart sliding down are in the anim folder.


Be sure to check out the complete source code on my GitHub account at the link at the top of the article. 


2 thoughts on “Understanding Android’s WebChromeClient onCreateWindow Method

  1. This is great, thanks so much for taking the time to post this. This was a huge help for me in my project.

    Any idea how one could go about allowing javascript to close the child window after it was opened?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s