Search code examples
javaandroidwebviewscreen-orientationtext-size

How to prevent webview from changing text size on orientation change?


I am dynamically adding webview's to a linear layout. My problem is when the orientation changes the text size also changes.For example when app is in landscape mode the text size is say large and in portrait mode it is medium. I want to keep text size medium on orientation change. This is the code I am trying to implement :-

private void handle_webcontent(String contentType, final String content) {
    String html_open = "<html><head><style>@font-face {font-family: 'verdana';src: url('file://"+ this.getFilesDir().getAbsolutePath()+ "/verdana.ttf');}body {font-family: 'verdana';}</style><title></title></head><body>";
    String html_close = "</body></html>";
    final WebView webView1;
    webView1 = new WebView(this);
    LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        layoutParam.setMargins(10,0,10,10);
        webView1.setLayoutParams(new LinearLayout.LayoutParams(layoutParam));

    }
    web_linearLayout.addView(webView1);
    switch (contentType) {
        case "text":
            if (content.equals("NA")) {
                Log.d("CONTENT", "NOTHING TO DISPLAY");

            } else {
                Document doc = Jsoup.parse(html_open);
                doc.append(content);
                doc.append(html_close);
                webView1.getSettings().setJavaScriptEnabled(true);
                webView1.setInitialScale(getScale());
                webView1.getSettings().setAllowFileAccess(true);
                webView1.getSettings().setLoadsImagesAutomatically(true);
                webView1.setWebViewClient(
                        new WebViewClient() {

                            @Override
                            public void onPageFinished(WebView view, String url) {
                                String javascriptCode = "javascript:";
                                javascriptCode+="var elements=document.querySelectorAll(\"code\");";
                                javascriptCode+="var parent;";
                                javascriptCode+="var container;";
                                javascriptCode+="for(i in elements){";
                                javascriptCode+="container = document.createElement('div');";
                                javascriptCode+="parent = elements[i].parentElement;";
                                javascriptCode+="parent.replaceChild(container, elements[i]);";
                                javascriptCode+="container.appendChild(elements[i]);";
                                javascriptCode+="container.setAttribute(\"style\",\" white-space: nowrap; background-color: #EEEEEE;  padding-top: 4px;  padding-right: 4px;  padding-bottom: 4px;  padding-left: 4px;\");}";
                                webView1.loadUrl(javascriptCode);
                            }
                        }
                );
                webView1.loadDataWithBaseURL("", String.valueOf(doc), "text/html", "UTF-8", "");
                Log.d("CODE CONTENT",String.valueOf(doc));


            }

            break;
        case "image":
            if (content.equals("NA")) {
                Log.d("CONTENT", "NOTHING TO DISPLAY");
            } else {
                String image = image_url + content;
                downloadImages(image);
            }
            break;
        case "button":
            if (content.equals("NA")) {
                Log.d("CONTENT", "NOTHING TO DISPLAY");

            } else {
                Button button = (Button) getLayoutInflater().inflate(R.layout.button_template, null);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
                button.setLayoutParams(layoutParams);
                web_linearLayout.addView(button);

                final WebView webView;
                webView = new WebView(this);
                webView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                web_linearLayout.addView(webView);


                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String html_open = "<html><head><style>@font-face {font-family: 'verdana';src: url('file://"+ DisplayContent.this.getFilesDir().getAbsolutePath()+ "/verdana.ttf');}body {font-family: 'verdana';}</style><title></title></head><body>";
                        String html_close = "</body></html>";
                        Document doc = Jsoup.parse(html_open);
                        doc.append(content);
                        doc.append(html_close);
                        webView.getSettings().setJavaScriptEnabled(true);
                        webView.getSettings().setAllowFileAccess(true);
                        webView.getSettings().setLoadsImagesAutomatically(true);
                        webView.loadDataWithBaseURL("", String.valueOf(doc), "text/html", "utf-8", "");

                    }
                });
            }

            break;
        default:
    }

}

Any help or suggestion is appreciated.Thank you.


Solution

  • In you activity mention this

    android:configChanges="keyboardHidden|orientation"
    

    or

    android:configChanges="orientation|screenSize"