Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display html formatted text in android app

I am porting a web application to android platform.The app will consist of some web content (accessed using WebView) and some offline content. I need to display some amount of text as offline content in the home screen, and I'm kinda confused how to go about it. I have gone through the android documentation of formatting text, but I found it a bit tedious to use when dealing with more than a few lines of text. So I am considering creating some HTML pages and including them in the res folder of my app. And then loading them using WebView. Is it advisable to use such an approach? or is there a better way out?

like image 966
Poonam Anthony Avatar asked Mar 04 '13 09:03

Poonam Anthony


Video Answer


2 Answers

Well for simple text you can always do following on a TextView:

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

If as you say it is more complicated (with images and/or css etc.) I'd recommend putting the html in the assets/ folder and load it from there directly into a WebView.

like image 175
Warpzit Avatar answered Oct 12 '22 12:10

Warpzit


There's the Html class which can format HTML text but it's quite limited. You can read the documentation here: http://developer.android.com/reference/android/text/Html.html

The method that you'll probably be most interested in is Html.fromHtml(java.lang.String). It returns an Spanned object which you can then use 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 then you'll most likely end up using a WebView.

like image 34
Paul Avatar answered Oct 12 '22 12:10

Paul