Display multiple lines of text in the Alert dialog box

I need to display multiple lines of messages, not just a single paragraph, in the alert dialog.

new AlertDialog.Builder(this) .setTitle("Place") .setMessage("Go there" + "Go here") .setNeutralButton("Go Back", null) .show(); 

Is there a way to get started with new lines? In the same way as clicking is entered after a sentence in Microsoft Word?

+4
source share
4 answers

There are no guarantees about this, but usually to do a few lines you do something like:

 .setMessage("Go there\nGo here"); 

"\ n" is the escape character, which means "New Line", which I do not know about your specific case, but you can use it in almost all AFAIK.

+12
source

Use the tag "\ n" in your lines:

 new AlertDialog.Builder(this) .setTitle("Place") .setMessage("Go there" + "\n" + "Go here") .setNeutralButton("Go Back", null) .show(); 
+4
source

Have you tried a carriage return or line feed character? These are characters 10 and 13, respectively (special characters \ n and \ r, or \ u000a and \ u000d)

0
source

I think the best solution is to avoid the interrupt string and use a custom view component.

 AlertDialog.Builder builder = new AlertDialog.Builder(this); TextView tw =new TextView(this); tw.setMaxLines(10); tw.setPadding(3,3,3,3); tw.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); tw.setSingleLine(false); tw.setText("ver long messagge \n with break line \n"); builder.setView(tw); 

With this solution, you can break the line if you want to use \n , but if you do not, the text will be wrapped in several lines (since I set tw.setSingleLine(false); ).

0
source

Source: https://habr.com/ru/post/1386396/


All Articles