Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webview doesn't display text with color

In my app I display some Html content in webview:

String webViewConent = 'this is some <span style="color:#2ecc71">sample</span> string'
webView.loadData(omowienie, "text/html; charset=utf-8", "UTF-8");

However after last app update, which was related with some other thing, for some users webview doesn't work correctly. They see only string which is before span tag. The problem is not related to any specific android version.

like image 316
mik.ro Avatar asked Feb 01 '19 16:02

mik.ro


People also ask

How do I display HTML content in WebView?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

Is WebView deprecated in Android?

This interface was deprecated in API level 12. This interface is now obsolete.

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.


Video Answer


2 Answers

Same problem here, I found base64 encoding as a quick fix:

String base64 = android.util.Base64.encodeToString(html.getBytes("UTF-8"), android.util.Base64.DEFAULT); mWebView.loadData(base64, "text/html; charset=utf-8", "base64"); 
like image 154
FloFo Avatar answered Sep 16 '22 22:09

FloFo


I had the same issue today. As a simple workaround I replaced hex colors in CSS by RGB equivalents.

Before

<span style="color: #3050c0">Some text</span>

After

<span style="color: rgb(48, 80, 192)">Some text</span>
like image 38
Digedag Avatar answered Sep 20 '22 22:09

Digedag