Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing the bottom Padding from a TextView while using HTML Format

Tags:

html

android

I try set my html text on TextView like this

my_text.setText(HtmlCompat.fromHtml("<p>This is the awesome place to gain</p><p><strong>awesomeness </strong>and <em>deliciuosness. </em>very<em> </em><u>nice</u></p>", HtmlCompat.FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH))

I try set the TextView with a border, And I got padding bottom like this.

enter image description here

How to remove that? Because My TextView doesn't set anything

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

    <TextView
        android:id="@+id/my_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/border_black_fill_white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
like image 361
newbie Avatar asked May 14 '19 07:05

newbie


2 Answers

In kotlin it's just using trim() method:

val stringHtml = "<p>This is the awesome place to gain</p><p><strong>awesomeness </strong>and <em>deliciuosness. </em>very<em> </em><u>nice</u></p>"

val spannedHtml = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Html.fromHtml(stringHtml, Html.FROM_HTML_MODE_COMPACT)
} else {
    Html.fromHtml(stringHtml)
}

my_text.text = spannedHtml.trim()
like image 108
Noelia Avatar answered Sep 20 '22 18:09

Noelia


This extra space that you see is infact line break followed by another line break.

When you dive into the Html.fromHtml(...) implementation which is used internally by HtmlCompat.fromHtml, you'll come across the following method that handles paragraph tags:

private static void handleP(SpannableStringBuilder text) {
    int len = text.length();

    if (len >= 1 && text.charAt(len - 1) == '\n') {
        if (len >= 2 && text.charAt(len - 2) == '\n') {
            return;
        }

        text.append("\n");
        return;
    }

    if (len != 0) {
        text.append("\n\n");
    }
}

So to handle this just trim the string so space added at the end gets removed.

 String html = "<p>This is the awesome place to gain</p><p><strong>awesomeness </strong>and <em>deliciuosness. </em>very<em> </em><u>nice</u></p>"

CharSequence trimmedString = trim(Html.fromHtml(html));
myText.setText(trimmedString);

public static CharSequence trim(CharSequence s) {
        int start = 0;
        int end = s.length();
        while (start < end && Character.isWhitespace(s.charAt(start))) {
            start++;
        }

        while (end > start && Character.isWhitespace(s.charAt(end - 1))) {
            end--;
        }

        return s.subSequence(start, end);
    }

This will give you the desired result.

enter image description here

like image 24
akhil choudhary Avatar answered Sep 19 '22 18:09

akhil choudhary