4

Is there any possibility to load html from res/raw into the TextView? I know I can use WebView, but damn transparency is not always working (not on every device)

Rahul
  • 10,457
  • 4
  • 35
  • 55
user2234594
  • 305
  • 1
  • 10
  • 19

3 Answers3

12
myTextView.setText(Html.fromHtml(readTxt()));     

//This function will return string which you can set in your textview. And that String have html codes so use Html.fromHtml

 private String readTxt() {
    InputStream inputStream = getResources().openRawResource(R.raw.My_html_file);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i;
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return byteArrayOutputStream.toString();
  }
Rahul
  • 10,457
  • 4
  • 35
  • 55
  • Thanks a lot ! The result shocked me :) it definately doesn't look like it looked in html – user2234594 Aug 24 '13 at 18:24
  • well yes, my issue is resolved - It's impossible to make textview's html look like webview's html - the text is formatted differentely – user2234594 Aug 24 '13 at 19:49
4

You can put your html content in a string resource and use it in your TextView by :

textView.setText(Html.fromHtml(getResource().getString(R.string.my_html)));

To format your HTML in strings.xml file, use that syntax :

<string name="my_html">
    <![CDATA[
    Your html content here
    ]]>
</string>
S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
0

For simple HTML content you can use .setText(Html.fromHtml("Hello world!")). Just need to load html file to string from raw.

Community
  • 1
  • 1
trebron
  • 356
  • 1
  • 4