anon
anon

Reputation:

Html.fromHtml(String) + Linebreak Problem

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?

Upvotes: 26

Views: 13843

Answers (4)

luoqii
luoqii

Reputation: 21

@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.

Upvotes: 1

rharvey
rharvey

Reputation: 2005

change

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

to

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

Upvotes: 46

stacker
stacker

Reputation: 68942

The Html linebreak tag is <br/>

Upvotes: 2

Aaron Digulla
Aaron Digulla

Reputation: 328566

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

Upvotes: 36

Related Questions