57

how to add web view inside dialog or popup window.

my web view hold URL WebView.loadurl() .when view added inside dialog it still moving to the browser.

I've been in android loading webview in dialog but is no example of how to do it? thanks

Community
  • 1
  • 1
roy.d
  • 1,030
  • 2
  • 16
  • 33

4 Answers4

136

Here is example:

AlertDialog.Builder alert = new AlertDialog.Builder(this); 
alert.setTitle("Title here");

WebView wv = new WebView(this);
wv.loadUrl("http:\\www.google.com");
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);

        return true;
    }
});

alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        dialog.dismiss();
    }
});
alert.show();
Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68
  • thanks it realy workoing now with alertdialog, why its now working with popupwindow also? – user1194088 just now – roy.d Feb 07 '12 at 08:58
  • What if I want to show a loading indicator while the page loads? – user1940676 Nov 24 '13 at 10:11
  • ı wanna know , how can you set gravity to center, because it comes gravity left by default – CompEng Aug 02 '16 at 09:11
  • This not working when try display google drive shared document. – Jorny Jun 06 '17 at 16:02
  • I was trying with dialog fragment but it was not working. Above solution worked but if anybody is aware how we can implement webview in dialog fragment. (In my case webview content was not able to display in dialog fragment most of the time). – brijexecon Jul 12 '18 at 10:08
  • Hi. Seems not to be working on AlertBuilder created dialog popup. – HX_unbanned Jan 05 '20 at 14:59
4

You need to override setWebViewClient()..

mWebView = (WebView) view.findViewById(R.id.wv1);
mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});
mWebView.loadUrl(mUrl);
Rajdeep Dua
  • 11,190
  • 2
  • 32
  • 22
2

If you are trying to display a webview in a popup then you must set the width and height of your linear layout in your popup's layout file (popup_layout.xml) as shown below.

You have to do this because popup's layout doesn't have any parent to refer for the size when you try using 'match_parent' or so.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:layout_width="400dp"
android:layout_height="400dp"
android:background="#FAFAFA"
android:id="@+id/popup_layout"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:id="@+id/txtclose"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_gravity="end"
    android:background="@color/colorPrimaryDark"
    android:text="X"
    android:textColor="@color/main_yellow"
    android:textStyle="bold" />

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/externalUrl"/>
</LinearLayout>
Apoorv Jain
  • 34
  • 1
  • 5
0

PopupWindow code:

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    try 
    { 
        int[] location = new int[2]; 

        (xml item where you want it to appear).getLocationOnScreen(location);
        //Get x and y positions
        p = new Point();
        p.x = location[0];
        p.y = location[1];
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}
private void popTab(final Activity myActivity) 
{
    popupWidth = 350;
    popupHeight = 600;

    popup.setWidth(popupWidth);
    popup.setHeight(popupHeight);

    // Inflate the popup_layout.xml
    LinearLayout viewGroup = (LinearLayout) myActivity.findViewById(R.id.myMainLayoutID);
    LayoutInflater layoutInflater = (LayoutInflater) myActivity
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layoutTab = layoutInflater.inflate(R.layout.mylayout, viewGroup);

    //Get webview from xml
    WebView wv = (WebView)layoutTab.findViewById(R.id.webView2);

    // Creating the PopupWindow
    final PopupWindow popup = new PopupWindow(layoutTab);
        //Set to view
    popup.setContentView(layoutTab);

        //Setup webview
    wv.loadUrl("http:\\www.google.com");
    wv.setWebViewClient(new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);

            return true;
        }
    });

    //Add some animation from style folder
    popup.update();
    popup.setAnimationStyle(R.style.Animation);
    popup.setFocusable(true);

    popup.showAtLocation(layoutTab, Gravity.NO_GRAVITY, p.x, p.y);
}

Use popTab() where ever you like. Hope this helps with popup window.

abotrix
  • 138
  • 1
  • 12