Displaying multiple lines of text and variables in an AlertDialog using setMessage()

Navigatron picture Navigatron · May 6, 2011 · Viewed 55.9k times · Source

I need to display multiple lines of text in an Alert Dialog. If I use multiple setMessage() methods, only the last setMessage is displayed, as shown below.

final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                alertDialog.setTitle("Statistics:");
                alertDialog.setMessage("No. of attempts: " + counter);
                alertDialog.setMessage("No. of wins: " + counterpos);
                alertDialog.setMessage("No. of losses: " + counterneg);

Is there a way to create a new line for each of these in the dialog? Like using \n in System.print.out(); method.

Thanks!

Answer

Kenny C picture Kenny C · May 6, 2011

You can do something like this

String alert1 = "No. of attempts: " + counter;
String alert2 = "No. of wins: " + counterpos;
String alert3 = "No. of losses: " + counterneg;
alertDialog.setMessage(alert1 +"\n"+ alert2 +"\n"+ alert3);