How to display .rtf file inside TextView?
Asked Answered
M

1

12

my app needs to display custom text saved in a bunch of .rtf files inside a TextView. I have searched in different forums and i still don't have a clear solution. It seems pretty easy right? i have developed a few apps like this in iOS and i had no problems displaying .rtf text inside a UITextView as an attributed string. I thought in Android will be easier. Thanks for your time

Mills answered 22/4, 2014 at 0:14 Comment(0)
F
19

It seems pretty easy right?

Not especially. Heck, even Windows largely abandoned RTF a decade or so ago.

Option #1: If you were planning on packaging the RTF in the app itself (e.g., as raw resources or assets), get rid of the RTF and use HTML in your app in the first place, using either Html.fromHtml() and a TextView or use a WebView.

Option #2: Find some Java code that can convert RTF to HTML on the fly, then proceed as in Option #1.

Option #3: Find some cross-architecture C/C++ code that can convert RTF to HTML on the fly, add that to your app via the NDK, then proceed as in Option #1.

Option #4: Roll your own RTF parser and a SpannableStringBuilder to create the formatted Spannable to load into the TextView.

Fiance answered 22/4, 2014 at 0:23 Comment(3)
Thanks! it seems that the answer will be use html instead of rtf, another question... how do i display html inside a textview? Thanks again.Mills
@VictorPinto: Simple HTML can be converted into a Spanned via Html.fromHtml(), and that can be passed to setText() on a TextView. The key is "simple" -- if it's a conversion from RTF to HTML, you'll probably be in OK shape.Fiance
Thanks i got the perfect solution! never thought i was going to need html -_-Mills

© 2022 - 2024 — McMap. All rights reserved.