0

I'm trying to load html with CSS which has ID selector and the file doesn't load to the webview as expected.

This is the file which I tried to load:

 <!DOCTYPE html> <html> <head> <style> #para1 { text-align: center; color: red; } </style> </head> <body> <p id="para1">Hello World!</p> <p>This paragraph is not affected by the style.</p> </body> </html> 

However when I tried the code below,it loads to the webview.

 <html> <head> <style> h1 {color:red;} p {color:blue;} </style> </head> <body> <h1>A heading</h1> <p>A paragraph.</p> </body> </html> 

I have enabled these properties on the WebView:

 mWebView.setWebViewClient(new CustomWebViewClient()); mWebView.setWebChromeClient(new CustomWebChromeClient()); mWebView.getSettings().setSupportZoom(true); // ZoomControls mWebView.getSettings().setBuiltInZoomControls(true); // Multitouch mWebView.getSettings().setDisplayZoomControls(false); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setDomStorageEnabled(true); mWebView.getSettings().setLoadWithOverviewMode(true); mWebView.getSettings().setUseWideViewPort(CONFIG.webviewEnableViewport()); mWebView.getSettings().setAllowFileAccess(true); mWebView.requestFocus(View.FOCUS_DOWN); mWebView.setHorizontalScrollBarEnabled(true); mWebView.setVerticalScrollBarEnabled(true); 

Can anyone help me on this?

3
  • Some info about the header in WebView on this link Commented Jul 21, 2021 at 21:57
  • What do you mean it is not working? For me it works exactly as I would expect it to work. not working is a quite useless description for us. you need to be way more clear then that. What is not working? what is expected? what result do you get? Commented Jul 21, 2021 at 23:31
  • @tacoshy I'm getting blank page for the first snippet Commented Jul 22, 2021 at 14:41

1 Answer 1

0

I could solve the issue by using the base64 encoding.

 String encodedHtml = Base64.encodeToString(myHtmlString.getBytes(), Base64.NO_PADDING); mWebView.loadData(encodedHtml, "text/html", "base64"); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.