Is there any other way to display HTML in Android TextView without using the webview method and Html.fromHtml ()?

I searched a lot. I need to render HTML code in Textview. But Mostly Html.fromHtml (source) used in Textview. But this method is not supported by all html tags. Only a few tags are supported by Html.fromHtml ().

Because I need to add all css and all html tag tags.

I want to display html code in Textview without using Webview. Is there any other way to implement this functionality.

+4
source share
5 answers

- html-, , .

android-summernote

0

. , TextView HTML-. : HTML-TextView.

+4

, , HTMLTagHandler ( Html.fromHtml()) ImageGetter . , , .

  • public class HtmlTagHandler implements Html.TagHandler {}
  • public final class GlideImageGetter implements Html.ImageGetter{}

Html.fromHtml(htmlText, new GlideImageGetter(webViewReferenceObj, 
(AppCompatActivity) mContext), new HtmlTagHandler())
+1
source

It's good that you dig deep into Html.fromHtml (). But there is only such a way to use html code, and also for a better look and layout, you can wrap your string file as follows.

<string><![CDATA[<html>YOUR STRING FILE</html>]></string>

Or simply you can use Webview.

0
source

I found this tutorial. You can use HTML and CSS with TextView without using WebView, but you should use Html.fromHtmlas below, and useSpanned

Give it a try!

Android Spannable Info See here

public class MainActivity extends ActionBarActivity {
    private TextView textView;
    private final String htmlWithCSS = "<!DOCTYPE html>\n" +
            "<html>\n" +
            "<head>\n" +
            "<style>\n" +
            "a1 {\n" +
            "    text-decoration: overline;\n" +
            "}\n" +
            "\n" +
            "a2 {\n" +
            "    text-decoration: line-through;\n" +
            "}\n" +
            "\n" +
            "a3 {\n" +
            "    text-decoration: underline;\n" +
            "}\n" +
            "</style>\n" +
            "</head>\n" +
            "<body>\n" +
            "\n" +
            "<a1>This is heading 1</a1>\n" +
            "<a2>This is heading 2</a2>\n" +
            "<a3>This is heading 3</a3>\n" +
            "\n" +
            "</body>\n" +
            "</html>\n";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = ((CustomTextView) findViewById(R.id.text_view));
        Spanned spanned = Html.fromHtml(htmlWithCSS);
        textView.setText(spanned);
    }
}
-1
source

Source: https://habr.com/ru/post/1661851/


All Articles