fromHtml method does not format html text properly on API level 28

728 views Asked by At

Basically, i am using Html.fromHtml method to display text in html format inside a textView. Everything renders correctly, except the bullets of the list items. They are being cutt-off just like this case (Bullet points in textview are cut off).

What i am seeing here(Bullet points in textview are cut off) and here (https://medium.com/ackee/how-to-make-bulletproof-bullet-lists-in-textview-223c54fb21e6), idicates that the BulletSpan class had some bugs so in later version (API lvl 28) they fixed it. However, among all the things i tried, i currently have: + API lvl 28 installed + compile and target sdk version in gradle --> 28 + min sdk version in gradle --> 24 + device that has android 7.0 (meaning: API lvl24)

and it still shows the bullets being cut off

main layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/basicTextView"
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:text="Hello World!"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

main activity java code:

import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import static android.text.Html.FROM_HTML_MODE_LEGACY;

public class MainActivity extends AppCompatActivity {

    private TextView textview, basicTextView;
    private Button confirmButton;
    private String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        basicTextView = (TextView) findViewById(R.id.basicTextView);

        text = "<ul>\n" +
                "   <li>Morbi in sem quis dui placerat ornare. Pellentesque odio nisi, euismod in, pharetra a, ultricies in, diam. Sed arcu. Cras consequat.</li>\n" +
                "   <li>Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus.</li>\n" +
                "   <li>Phasellus ultrices nulla quis nibh. Quisque a lectus. Donec consectetuer ligula vulputate sem tristique cursus. Nam nulla quam, gravida non, commodo a, sodales sit amet, nisi.</li>\n" +
                "   <li>Pellentesque fermentum dolor. Aliquam quam lectus, facilisis auctor, ultrices ut, elementum vulputate, nunc.</li>\n" +
                "</ul>" +
                "<nav>\n" +
                "  <ul>\n" +
                "    <li><a href=\"#\">Home</a></li>\n" +
                "    <li><a href=\"#\">About</a></li>\n" +
                "    <li><a href=\"#\">Clients</a></li>\n" +
                "    <li><a href=\"#\">Contact Us</a></li>\n" +
                "  </ul>\n" +
                "</nav>" +
                "<h1>Main Title</h1>\n" +
                "        <h2>A sub-title</h2>\n" +
                "        <p>This is some html. Look, here\\'s an <u>underline</u>.</p>\n" +
                "        <p>Look, this is <em>emphasized.</em> And here\\'s some <b>bold</b>.</p>\n" +
                "        <p>This is a UL list:\n" +
                "        <ul>\n" +
                "        <li>One</li>\n" +
                "        <li>Two</li>\n" +
                "        <li>Three</li>\n" +
                "        </ul>\n" +
                "        <p>This is an OL list:\n" +
                "        <ol>\n" +
                "        <li>One</li>\n" +
                "        <li>Two</li>\n" +
                "        <li>Three</li>\n" +
                "        </ol>";

    basicTextView.setText(Html.fromHtml(text, FROM_HTML_MODE_LEGACY));
    }
}

So, even though it should be rendering list bullets properly, it still doesn't. I have the proper API lvl installed in Android Studio so i have no idea how should i approach this issue. If theres something that is unclear please ask. Any ideas?

1

There are 1 answers

0
Iraklis Inguana On BEST ANSWER

So, apparently it plays properly if the device lvl is at least 27 or higher. I thought that if compile and target sdk version were high enough in gradle, the fixed version of the BulletSpan class would apply on the device, even though the device is lower lvl. This speculation is fundamentally wrong since the API lvl of the app that will be installed is the same as the API lvl supported by the device. That means if the device supports lower API lvl, then the version of the bugged BulletSpan class will be installed.