Handle Redirect Window In Android Webview

Updated 12 June 2023

Save

To handle the redirect window in android Webview we have to override the “onCreateWindow” of WebChromeClient().

Webview

Android Webview is used to display HTML content or to load webpage pages in the android app.

WebView uses a WebKit engine to display web pages.
To load the webpage in Webview loadUrl() and loadData() methods are used.

Sometimes some web pages redirect to another webpage or another window on any click event.
In Webview either the redirected window will not open directly.
To show redirect pages in Webview we need to write some extra code.

Let’s take an example.

To show the webpage in our sample app we have written some HTML code and added that file to the assets folder.

to-open-the-redirected-window-in-android-webview-we-have-to-override-oncreatewindow-of-webchromeclient
Fig-1

Let’s have look at the main activity code “MainActivity.kt”.

to-open-the-redirected-window-in-android-webview-we-have-to-override-oncreatewindow-of-webchromeclient
Fig-2

Now if we click on the button then it will not redirect/open any new window.

To open the redirected window we have to override “onCreateWindow” of WebChromeClient().

After overriding the method, we have to create another Webview and a Dialog or view according to the requirement.
Complete Main Activity Code :

After adding the above code to your app you are able to open the redirect window in your hybrid app.

to-open-the-redirected-window-in-android-webview-we-have-to-override-oncreatewindow-of-webchromeclient

Conclusion


In this blog, we see how we can open a redirected window in our android hybrid app or Webview.

To learn more about Webview, you can also refer to this link -> Android Webview

Thanks for Reading.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


4 comments

  • Emma
    Hi
    According to the picture am seeing on this tutorial, it taught to pop up a new windows over existing one. Can you guides how to redirect directly into html file without any layer? I mean a window page not being over another?
    • Pankaj Tyagi (Moderator)
      You can directly load your HTML file by loading that HTML file URL in Webview.
      like- web_view.loadUrl(“file:///android_asset/test.html”);

      If you don’t want to open new window then Please use WebSettings.setSupportMultipleWindows(false).

  • Jide
    Please, can I get a complete code for this? I am not getting it Like I need the full Import code please
    • Pankaj Tyagi (Moderator)
      Due to company policy, we can’t provide the full project code.
      You can share the issue or error you are facing with us here.
      We will check and guide you.
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home