Html.fromHtml(String) + Linebreak Problem

11,692

Solution 1

Try <br /> (note the slash is after the tag name plus it's a forward slash, not a backward one).

Solution 2

change

builder.setMessage(Html.fromHtml(message));

to

builder.setMessage(Html.fromHtml(message.replace("\n","<br />"));

Solution 3

The Html linebreak tag is <br/>

Solution 4

@Aaron Digulla it not work for me , but i find this: https://developer.android.com/reference/android/text/Html.html#FROM_HTML_MODE_LEGACY

replace "\n" with "\n\n"

it work for me.

Share:
11,692

Related videos on Youtube

Aaron Digulla
Author by

Aaron Digulla

I'm a software developer living in Switzerland. You can reach me at digulla at hepe dot com.

Updated on April 28, 2022

Comments

  • Aaron Digulla
    Aaron Digulla almost 2 years

    I'd like to display a text with a linebreak in an Alert Message:

    private void showAbout() {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
    
            String message = "<b>Rechtlicher Hinweis:</b>\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a dolor sapien. Etiam arcu erat, lobortis sed vestibulum ut, adipiscing et enim. Nulla vestibulum volutpat dolor, non pellentesque purus imperdiet vitae. Aenean et elit vel erat consectetur pulvinar. Sed semper, ante vel elementum aliquet, dui urna scelerisque tortor, eu auctor lorem nunc adipiscing velit. Praesent eget libero diam, eget imperdiet sem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae.\n" + getVersionInfo();
    
            builder.setMessage(Html.fromHtml(message));
            builder.setCancelable(false);
            builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            builder.setTitle("About"); 
            builder.setIcon(R.drawable.icon); 
    
            AlertDialog alert = builder.create(); 
    
            alert.show(); 
        }
    

    However this works more or less fine, the linebreaks

    \n

    aren't displayed at all. I already tried to replace \n by \\n or even <\br> but nothing worked. Any hints how to do this?