21

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.

mik.ro
  • 4,381
  • 2
  • 18
  • 23
  • I had the same problem in an app of mine, upvoted! – Nicola Gallazzi Feb 01 '19 at 16:06
  • Did you try with sample? – Nicola Gallazzi Feb 01 '19 at 16:07
  • No, the html content is created through wysiwyg editor on my website and it doesn't allow font tag... When did you encounter that problem? – mik.ro Feb 01 '19 at 16:14
  • I see.. I had the same problem with this string: <body style = \'font-family: Montserrat; text-align: center; line-height: 1.5em\'><span style =\'color: #FFFFFF; font-size: 16pt\'>You have</font><span style =\'color: #00B460; font-size: 22pt\'> %1$d </span><span style =\'color: #FFFFFF; font-size: 16pt\'>seconds left to close the lock</span> using Html.fromHtml function. On some devices was working, on others not – Nicola Gallazzi Feb 01 '19 at 16:30
  • @NicolaGallazzi seems it is caused by webview update – mik.ro Feb 01 '19 at 23:02
  • @NicolaGallazzi doesn't work with font tag. I had to remove: style="color:#2ecc71" – mik.ro Feb 02 '19 at 08:14

7 Answers7

27

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");
FloFo
  • 271
  • 2
  • 2
7

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>
Digedag
  • 101
  • 1
  • 2
  • That specifically means the number sign (U+0023) is the issue, right? Is it a blatant parser error? I wonder if it’s documented or intentional. – dakab Oct 20 '21 at 08:30
3

I found the approach in this answer on WebView encoding seems to fix this issue. Instead of using loadData, try loadDataWithBaseURL:

webview.loadDataWithBaseURL(null, html, "text/html; charset=utf-8", "utf8", null)
David Tchepak
  • 9,826
  • 2
  • 56
  • 68
0

I have same problem. My app does not display text after span tag. Some devices works fine but some devices does not work. Upvoted!

<p><span style="color:#0000CD"><strong>Materials:</strong></span></p>

As a result of my research and experimentation, I have disabled the Android System Webview application on my device. As a result of this process, my application worked correctly. However, to me, this cannot be a solution. What are your ideas?

jancooth
  • 555
  • 1
  • 10
  • 25
  • when did you encounter the problem for the first time ? In my case it was 2 days ago – mik.ro Feb 01 '19 at 19:49
  • @mik.ro yes, This have been started 2 days ago, and still goes on. I am looking for solve this problem but i can not find a way. I hope we solve this. – jancooth Feb 01 '19 at 19:50
  • I think, this problem occured Android System Webview. I have two devices. I checked their android system webview application version and i saw that their versions different. And also, Google has published new version on 29 January 2019. But i do not know how we can solve this. – jancooth Feb 01 '19 at 20:07
  • In my other app the webview also doesn't work in some cases. I'm pretty sure it is connected to the latest webview update – mik.ro Feb 01 '19 at 22:47
0

I think the problem is the new update in Android Webview System. The old version works for me.

My alternative solution here is to uninstall Android Webview System in your phone so that you can have the default version(this solution is not ideal if your app is uploaded in Google Playstore and have a lot of users)

The second option is you can delete your color and background color codes in html for the meantime and it will work(I think it is Android Webview System's bug and we just need to wait until they fix their issues)

Angelo Lucena
  • 209
  • 1
  • 3
  • 13
0

I have same problem. It may be the Android WebView System's bug but the solution is using font tag to color the texts.

String webViewConent = "this is some <font style="color:green">sample</font> string";

Note : This code is not working for Hex format colors

0

I din't find a fix but I was able to do a work around changing the string with the original HTML. This might help someone else, AS LONG AS YOU ALREADY KNOW THE COLOURS YOUR ARE GOING TO DISPLAY:

String newString = originalString.replaceAll("color:#888888", "color:gray");
Roney Sampaio
  • 320
  • 3
  • 7