How to insert a new line in strings in Android

AndroidStringEnter

Android Problem Overview


I'm creating an android application and within it there is a button that will send some information in an email, and I don't want to have everything all in one paragraph.

Here's what my app is doing for the putExtra for the email's body:

I am the first part of the info being emailed. I am the second part. I am the third part.

Here's what I want it to do:

I am the first part of the info being emailed.
I am the second part.

I am the third part.

How would I put a new line into a string or with the putExtra method to accomplish that?

Android Solutions


Solution 1 - Android

Try:

String str = "my string \n my other string";

When printed you will get:

my string
my other string

Solution 2 - Android

Try using System.getProperty("line.separator") to get a new line.

Solution 3 - Android

I would personally prefer using "\n". This just puts a line break in Linux or Android.

For example,

String str = "I am the first part of the info being emailed.\nI am the second part.\n\nI am the third part.";

Output

I am the first part of the info being emailed.
I am the second part.

I am the third part.

A more generalized way would be to use,

System.getProperty("line.separator")

For example,

String str = "I am the first part of the info being emailed." + System.getProperty("line.separator") + "I am the second part." + System.getProperty("line.separator") + System.getProperty("line.separator") + "I am the third part.";

brings the same output as above. Here, the static getProperty() method of the System class can be used to get the "line.seperator" for the particular OS.

But this is not necessary at all, as the OS here is fixed, that is, Android. So, calling a method every time is a heavy and unnecessary operation.

Moreover, this also increases your code length and makes it look kind of messy. A "\n" is sweet and simple.

Solution 4 - Android

I use <br> in a CDATA tag. As an example, my strings.xml file contains an item like this:

<item><![CDATA[<b>My name is John</b><br>Nice to meet you]]></item>

and prints

My name is John
Nice to meet you

Solution 5 - Android

If you want to add line break at runtime into a String from same string you are deriving the value then this Kotlin code works for me:

str = "<br>"+str?.replace("," , "</br><br>")+"</br>"
value = HtmlCompat.fromHtml(${skill_et_1}",Html.FROM_HTML_MODE_LEGACY)
tv.text = value

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionReedView Question on Stackoverflow
Solution 1 - Androiduser824013View Answer on Stackoverflow
Solution 2 - AndroidkenoView Answer on Stackoverflow
Solution 3 - AndroidAritra RoyView Answer on Stackoverflow
Solution 4 - AndroidmarcolavView Answer on Stackoverflow
Solution 5 - AndroidMohit KumarView Answer on Stackoverflow