48

I am trying to place html into a TextView. Everything works perfectly, this is my code.

String htmlTxt = "<p>Hellllo</p>"; // the html is form an API
Spanned html = Html.fromHtml(htmlTxt);
myTextView.setText(html);

This sets my TextView with the correct html. But my problem is, having a

tag in the html, the result text that goes into the TextView has a "\n" at the end, so it pushes my TextView's height higher than it should be.

Since its a Spanned variable, I can't apply regex replace to remove the "\n", and if I was to convert it into a string, then apply regex, I lose the functionality of having html anchors to work properly.

Does anyone know any solutions to remove the ending linebreak(s) from a "Spanned" variable?

AlexCheuk
  • 5,595
  • 6
  • 30
  • 35
  • Can you use actual HTML line breaks (`
    `) instead? You can place the line break before the text, and nothing after so it's:
    Hellllllllooooo , which will give it a break above the text, but nothing below it.
    – Ben Ashton Mar 06 '12 at 18:09
  • 1
    @Ben, the html return is from an API and I have no control over it. – AlexCheuk Mar 06 '12 at 19:06
  • 1
    Maybe using [TextUtils.getTrimmedLength()](http://developer.android.com/reference/android/text/TextUtils.html#getTrimmedLength%28java.lang.CharSequence%29) would work. There are a bunch of other functions in [TextUtils](http://developer.android.com/reference/android/text/TextUtils.html) that may help you solve the problem... – Salil Pandit Mar 06 '12 at 19:19
  • I've been battling this today. The answers below work fine as I wrote my own. The problem is when tags are not just in the end but elsewhere in the text. The only way to do it is to convert to a string and do .replace(...) but then you loose all your formatting. – JonWillis Sep 21 '12 at 16:08
  • check this too , very simple way and works ;) http://stackoverflow.com/a/24553061/2626219 – Hamid Qasemy Jul 03 '14 at 12:01

4 Answers4

62

Nice answer @Christine. I wrote a similar function to remove trailing whitespace from a CharSequence this afternoon:

/** Trims trailing whitespace. Removes any of these characters:
 * 0009, HORIZONTAL TABULATION
 * 000A, LINE FEED
 * 000B, VERTICAL TABULATION
 * 000C, FORM FEED
 * 000D, CARRIAGE RETURN
 * 001C, FILE SEPARATOR
 * 001D, GROUP SEPARATOR
 * 001E, RECORD SEPARATOR
 * 001F, UNIT SEPARATOR
 * @return "" if source is null, otherwise string with all trailing whitespace removed
 */
public static CharSequence trimTrailingWhitespace(CharSequence source) {

    if(source == null)
        return "";

    int i = source.length();

    // loop back to the first non-whitespace character
    while(--i >= 0 && Character.isWhitespace(source.charAt(i))) {
    }

    return source.subSequence(0, i+1);
}
Lorne Laliberte
  • 6,261
  • 2
  • 35
  • 36
  • 1
    This is one place where IMO a `do-while` is more readable than a `while`: `do { --i; } while (i >= 0 && Character.isWhitespace(cs.charAt(i)))` (obviously, split it up into multiple lines -- StackOverflow won't let me do that in a comment) – Vicky Chijwani Jan 17 '16 at 05:42
  • 1
    I had issues using CharSequence so had to cast it back as Spanned e.g. temp = (Spanned) temp.subSequence(0, temp.length() - 2); // has "\n\n" at the end – user1228123 Apr 10 '18 at 21:27
22

The spannable is a CharSequence, which you can manipulate.

This works:

    myTextView.setText(noTrailingwhiteLines(html));

    private CharSequence noTrailingwhiteLines(CharSequence text) {

        while (text.charAt(text.length() - 1) == '\n') {
            text = text.subSequence(0, text.length() - 1);
        }
        return text;
    }
Christine
  • 5,617
  • 4
  • 38
  • 61
  • This is quite inefficient for `SpannableString`s, as `subSequence()` creates a whole new `SpannableString` object. The accepted answer is much better. – Vicky Chijwani Jan 17 '16 at 05:35
  • When you make a tradeoff between programmer's time, code readability and program efficiency, it depends on the circumstances which version is better. I consider program efficiency the least relevant parameter, unless your code gets executed a thousand times per second on millions of devices. – Christine Jan 17 '16 at 12:58
11

You can try this:

Spanned htmlDescription = Html.fromHtml(textWithHtml);
String descriptionWithOutExtraSpace = new String(htmlDescription.toString()).trim();

textView.setText(htmlDescription.subSequence(0, descriptionWithOutExtraSpace.length()));
Gediminas Masaitis
  • 3,172
  • 14
  • 35
Juan Ocampo
  • 111
  • 1
  • 3
2

you can use this lines ... totally works ;)

i know your problem solved but maybe some one find this useful .

try{
        string= replceLast(string,"<p dir=\"ltr\">", "");
        string=replceLast(string,"</p>", "");
}catch (Exception e) {}

and here is replaceLast ...

public String replceLast(String yourString, String frist,String second)
{
    StringBuilder b = new StringBuilder(yourString);
    b.replace(yourString.lastIndexOf(frist), yourString.lastIndexOf(frist)+frist.length(),second );
    return b.toString();
}
Hamid Qasemy
  • 53
  • 2
  • 7