Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove extra line breaks after Html.fromHtml()

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?

like image 926
AlexCheuk Avatar asked Mar 06 '12 18:03

AlexCheuk


4 Answers

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);
}
like image 78
Lorne Laliberte Avatar answered Nov 03 '22 06:11

Lorne Laliberte


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;
    }
like image 24
Christine Avatar answered Nov 03 '22 04:11

Christine


You can try this:

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

textView.setText(htmlDescription.subSequence(0, descriptionWithOutExtraSpace.length()));
like image 11
Juan Ocampo Avatar answered Nov 03 '22 04:11

Juan Ocampo


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();
}
like image 2
Hamid Qasemy Avatar answered Nov 03 '22 05:11

Hamid Qasemy