Search code examples
androidwebviewtextview

How to load HTML content to Textview in Android


I am trying to add the html content to Android textview. I am using the following code to load.

TextView textView;

    String text = "<span style='color:#6D3078;font-family:'Quicksand',Medium;line-height:14px;font-size: 14px;font-weight:700;Width: 128px;Height:1px;Top:421px;Left:20px'><strong>Additional</strong> <strong>Benefits</strong></span><div style=\"border: 1px solid Gainsboro; height: 1px; width: 320px;\"></div><ul style=\"list-style: none; padding: 0;\"><li><span style=\"display: inline-block; width: 6px; height: 6px; border-radius: 50%; background-color: #6D3078; margin-right: 10px;\"></span><span style='color:#3F3F3F;font-family:' Quicksand ',Medium;line-height:12px;font-size: 12px;font-weight:500;Width: 320px;Height:12px;'>Program</span></li><li><span style=\"display: inline-block; width: 6px; height: 6px; border-radius: 50%; background-color: #6D3078; margin-right: 10px;\"></span><span style='color:#3F3F3F;font-family:' Quicksand ',Medium;line-height:12px;font-size: 12px;font-weight:500;Width: 320px;Height:12px;'>Individual</span></li><li><span style=\"display: inline-block; width: 6px; height: 6px; border-radius: 50%; background-color: #6D3078; margin-right: 10px;\"></span><span style='color:#3F3F3F;font-family:' Quicksand ',Medium;line-height:12px;font-size: 12px;font-weight:500;Width: 320px;Height:12px;'>Self</span></li><li><span style=\"display: inline-block; width: 6px; height: 6px; border-radius: 50%; background-color: #6D3078; margin-right: 10px;\"></span><span style='color:#3F3F3F;font-family:' Quicksand ',Medium;line-height:12px;font-size: 12px;font-weight:500;Width: 320px;Height:12px;'>Common</span></li></ul><a href=\"https://www.google.com\"><span style='color:#F37435;font-family:' Quicksand ',Medium;line-height:12px;font-size: 12px;font-weight:500;Width: 190px;Height:12px;'>Other Details & Program</span></a><p> </p><span style='color:#6D3078;font-family:' Quicksand ',Medium;line-height:14px;font-size: 14px;font-weight:700;Width: 148px;Height:14px;Top:421px;Left:20px'><strong>For Program</strong></span><p> </p><p> </p><p>Come with family <strong>Just</strong> Join with us.</p><p>\n";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textview_android);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));
        } else {
            textView.setText(Html.fromHtml(text));
        }
    }

But some styles are not applied.

Output:

enter image description here

But When I load the same html text in webview I am getting the following output.

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            myWebView = findViewById(R.id.webView);
            myWebView.loadUrl("file:///android_asset/myfile.html");
        }

Expected Output:

enter image description here

Please give some ideas to apply all styles in textview.


Solution

  • you simply can't

    TextView and Html.formHtml(..) are supporting only some of tags, take a look in HERE which. its basically a quick workaround for handling most common tags, like b or u, for fast text formatting (instead of using Spannables, which are way more complex and need more code to write)

    if you need more complex HTML handling you have to use WebView