2

I had a problem when you want to add webview in programmatically I've tried like this, but webview can not appear when the button was already able to appear I need a solution to this case

    //        sizeView
        LinearLayout.LayoutParams sizeView = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        );


//        viewButton
        Button home = new Button(this);
        home.setText("HOME");

        Button profile = new Button(this);
        profile.setText("PROFILE");

        Button academic = new Button(this);
        academic.setText("ACADEMIC");

        Button register = new Button(this);
        register.setText("REGISTER");

        Button about = new Button(this);
        about.setText("ABOUT");

//        viewWEB
        WebView webView = new WebView(this);


//        sideLeft
        LinearLayout sideLeft = new LinearLayout(this);
        sideLeft.setOrientation(LinearLayout.VERTICAL);
        sideLeft.setLayoutParams(sizeView);
        sideLeft.setBackgroundColor(Color.GRAY);
        sideLeft.addView(home);
        sideLeft.addView(profile);
        sideLeft.addView(academic);
        sideLeft.addView(register);
        sideLeft.addView(about);

//        wrapper
        LinearLayout layWrap = new LinearLayout(this);
        layWrap.setBackgroundColor(Color.BLUE);
        layWrap.setOrientation(LinearLayout.HORIZONTAL);
        layWrap.addView(sideLeft);
        layWrap.addView(webView);
        setContentView(layWrap);

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • refer http://stackoverflow.com/questions/11221777/webview-in-programmatically-created-relativelayout – sasikumar Nov 02 '16 at 10:05
  • Post a screenshot of your rendered layout... Are you seeing the blue background at all ? – Alex Nov 02 '16 at 10:07

2 Answers2

2

Your WebView is correctly getting rendered. Just enable required functionality like make it javascript enabled and set chrome client and loadUrl to see it working...

Meanwhile you can also test it by adding below line:

webView.loadData("<html><body>TEST</body></html>", "text/html", "utf-8");
Myth
  • 1,218
  • 12
  • 15
0
    LinearLayout layWrap = new LinearLayout(this);
    layWrap.setBackgroundColor(Color.BLUE);
    layWrap.setOrientation(LinearLayout.HORIZONTAL);
    layWrap.addView(sideLeft);

    setContentView(layWrap);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
    FrameLayout.LayoutParams linearlayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
    layWrap.setLayoutParams(linearlayoutParams);

    layWrap.addView(webView);

    webView.setLayoutParams(layoutParams);
    webView.setBackgroundColor(Color.RED);
    // to enable javascripts
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setBuiltInZoomControls(true);
    // zoom if you want
    webView.getSettings().setSupportZoom(true);
    // to support url redirections
    webView.setWebViewClient(new WebViewClient());
    // extra settings
    webView.getSettings().setLoadWithOverviewMode(false);
    webView.getSettings().setUseWideViewPort(true);
    webView.setScrollContainer(true);
    // setting for lollipop and above
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
        webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }

    webView.loadUrl("http://www.facebook.com");

done loading webview

Parth Dave
  • 2,096
  • 13
  • 20