WebView load from String, the good way (the working way!)

Thibault de Lambilly
AndroidPub
Published in
2 min readJun 11, 2018

My aim to write on Medium is to share my experiences gained on my projects, share my joy on tech I try and use. Moreover the “how I did” compare to the many “another word to the official documentation”

Today, it is about a huge frustration 😡, I had on my latest project (and had on the previous one also but less intense), as well as my personal note to not to run into the same trap again.

So, I just wanted to display formatted text in HTML on my Android App. I don’t want to use TextView because it has fewer format possibilities. The solution is then WebView, that open many more options. Sounds easy, isn’t it!

Quick search to how to do html text in a WebView , you find everywhere to do it like this:

myWebView.load(myHtmlString, "text/html", "utf-8")

It is working as long as you load it only once! 😱

As I am willing to change the text according to an event, I was stuck to understand why the WebView never refreshes its data.

After hours (yes, hours) of search why, tried many solutions found on several forums.

The solution was here:

Avoid WebView#loadData(String data, String mimeType, String encoding) it’s buggy.

Use WebView#loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) instead.

All my thanks and gratitudes go to M. Usman Khan, for his (2103!) answer that saved me from a serious break-down. 😙

Note to myself: to type inline code, type 3 backtick (`), hit enter, then go back between the first and the second one, and the inline code is then took into account, delete the backtick remaining.

Does that sh%tt* way is only with French keyboard ?? 💩 💩

--

--

Thibault de Lambilly
AndroidPub

Mobile enthusiast, Kotlin lover | full stack by pleasure