Display html formatted text in android application

I am porting a web application to the Android platform. The application will consist of some web content (access via WebView ) and some offline content. I need to display some text as standalone content on the main screen, and I'm a little confused on how to do this. I looked at the formatting documentation in android, but it was a little tedious to use it when working with multiple lines of text. Therefore, I am considering creating some HTML pages and including them in the res folder of my application. Then load them using WebView . Is it appropriate to use this approach? or is there a better way out?

+6
source share
2 answers

Good for plain text, you can always do the following on a TextView :

 someTextView.setText(Html.fromHtml(htmlAsAString)); 

If you say this is more complicated (with images and / or css, etc.), I would recommend placing html in a directory / folder and loading it directly into WebView .

+9
source

There is an Html class that can format HTML text, but it is rather limited. You can read the documentation here: http://developer.android.com/reference/android/text/Html.html

The most interesting method for you is Html.fromHtml(java.lang.String) . It returns a Spanned object, which can then be used to populate a TextView by simply calling textView.setText(..) .

If you find that it does not provide the required support for the HTML tags that you need, you will most likely end up using WebView .

+4
source

All Articles