JackFinch
JackFinch

Reputation: 48

WebView not loading URL in Dialog

I am trying to make an Android dialog that shows a webview:

final Dialog dialog = new Dialog(mActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.layout);
WebView webView = (WebView)dialog.findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient());

String customHtml = "<html><body>WebViewTest</body></html>";
webView.loadData(customHtml, "text/html", "UTF-8");
dialog.show();

But nothing is happening. It appears the webview has no size

I have been asked for the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="300dp"
    android:padding="10sp"
    android:background="#0ff"
    android:orientation="vertical">
    <WebView
        android:background="#f0f"
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

Upvotes: 2

Views: 1444

Answers (2)

Sasi Kumar
Sasi Kumar

Reputation: 13288

final Dialog dialog = new Dialog(mActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.layout);
WebView webView = (WebView)dialog.findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient());
String customHtml = "<html><body>WebViewTest</body></html>";
webView.loadData(customHtml, "text/html; charset=UTF-8", null);
dialog.show();

Upvotes: 0

Quintin B
Quintin B

Reputation: 5881

I would suggest looking here: android: webview inside dialog or popup

Rather use an AlertDialog, it will better suite your needs:

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();

Upvotes: 1

Related Questions