Gabi Radu
Gabi Radu

Reputation: 1107

Android WebView is not shown

I am trying to open an url in a new webview (created in a non activity class).

When debugging the mContext is not null, and i am on the main thread. I can see the toast and the last print but the webview is not shown. I don't understand what i am doing wrong.. can you spot a mistake? Thank you

mContext.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(mContext, "test", Toast.LENGTH_LONG).show();
                System.out.println("creating a new webview");
                WebView wv = new WebView(mContext);
                wv.loadUrl("urlhere");
                wv.getSettings().setJavaScriptEnabled(true);
                wv.setWebViewClient(new WebViewClient() {
                    public void onPageFinished(WebView view, String url) {
                        System.out.println("finished loading url: " + url);
                    }

                    public void onLoadResource(WebView view, String url) {
                    }

                    public boolean shouldOverrideUrlLoading(WebView view,
                            String url) {

                        return true;
                    }
                });
                wv.setVisibility(View.VISIBLE);
                System.out.println("should see the webview now");
            }
        });

Upvotes: 0

Views: 65

Answers (1)

greenapps
greenapps

Reputation: 11224

You are creating a new WebView but not giving it a parent where it can attach and display. Or use it in a setContentView.

Upvotes: 1

Related Questions