Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java android - how to set html from resources to TextView?

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)

like image 720
user2234594 Avatar asked Dec 07 '22 05:12

user2234594


1 Answers

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();
  }
like image 150
Rahul Avatar answered Dec 15 '22 00:12

Rahul