Set Webview to a minimum size programatically

19 Views Asked by At

[Android, Java] I have an alertdialog with a cancel button (that closes the alertdialog) as well as a webview. I am trying to set the webview so it matches the parent, because whenever I open up a webpage without a certain set size, it shrinks down to the smallest height possible. I have tried to set the webview's parameterlayout as well as is minimum height to no avail. My code:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Age Verification");

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int webViewHeight = (int) Math.round(0.8 * displaymetrics.heightPixels);
        int webViewWidth = (int) Math.round(0.7 * displaymetrics.widthPixels);
        Log.d("Intended min size: ", webViewHeight + ", " + webViewWidth);

        WebView wv = new WebView(this);
        wv.loadUrl({redacted});
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setVerticalScrollBarEnabled(true);
        wv.setHorizontalScrollBarEnabled(true);
        wv.setMinimumHeight(webViewHeight);
        wv.setMinimumWidth(webViewWidth);
        wv.getSettings().setLoadWithOverviewMode(true);
        wv.getSettings().setUseWideViewPort(true);
        wv.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                request.grant(request.getResources());
            }
        });
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest url) {
                view.loadUrl(url.getUrl().toString());
                if(url.getUrl().toString().contains("/close")){
                    finish();
                }
                return true;
            }
        });

        wv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        alert.setView(wv);
        alert.setNegativeButton("Close (Will void transaction if verification incomplete)", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                startTransactionFailureDialog(VERIFICATION_FAILED);
                dialog.dismiss();
            }
        });
        AlertDialog result = alert.show();

        Window window = result.getWindow();
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

The alertdialog fills up the screen, but just not the webview on certain sites (I am trying to force it to fill up the alert dialog, even if the site take us less space)

What it looks like now:

enter image description here

1

There are 1 best solutions below

0
mahdi_k_ch On

try this for maximum size of your WebView:

webView.setLayoutParams(new RelativeLayout.LayoutParams(800, 600));