javaandroidwebviewdialoglayout-inflater

how to load existing webview in alert dialog android


I have loaded a webview by default, with settings and url at the bottom of the page, i need to display that existing webview in an alert dialog box. I don't want to create new webview since i need to maintain the state of the existing webview (i mean if i had clicked on the button inside the webivew and toggled the content, then i need to show the exact same content in alert dialog) I tried to inflate by moving the webview along with some textview in seperate layout but it on alert dialog it shows textview but webview is not loaded. Suggestions welcome!!


Solution

  • To be clear, i assume your question is to reuse an existing web view from a layout and display it in alert dialog box. If so you can remove web view from the original layout and add it in alert dialog as follows

    WebView myWebview = findViewById(R.id.myWebView);
    ViewGroup parent = (ViewGroup) myWebview.getParent();
    if (parent != null) {                            
       parent.removeView(myWebview);
    }
    AlertDialog.Builder builder;
    AlertDialog alert;
    builder = new AlertDialog.Builder(this);
    alert = builder.create();
    alert.setView(myWebview);
    alert.show();
    

    so the existing webview will be removed from its layout and displayed on the alert dialog. You can also do the vice versa (remove webview from alert dialog and append in the previous layout.