String Resource
New Line Issue
Android Development
Text Formatting
Troubleshooting

String Resource new line /n not possible?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Android string resources (XML), writing \n as a literal backslash-n does not produce a newline. Instead, use the actual newline character in the XML or the XML entity &#10;. The \n escape is a Java/Kotlin string literal syntax, not an XML syntax. Android's resource system does handle \n in string resources in some contexts, but the behavior varies depending on how the string is loaded. The most reliable approach is to use \n within the <string> tag (Android's resource parser treats it as a newline) or to split the text using separate strings.

The Problem

xml
1<!-- strings.xml -->
2
3<!-- This DOES work — Android treats \n as newline in string resources -->
4<string name="greeting">Hello\nWorld</string>
5
6<!-- This also works — literal newline in XML -->
7<string name="multiline">Hello
8World</string>
9
10<!-- This does NOT work — HTML line break is ignored in plain text -->
11<string name="greeting_br">Hello<br/>World</string>
kotlin
1// Output of greeting: "Hello\nWorld" rendered as:
2// Hello
3// World
4
5val text = getString(R.string.greeting)
6textView.text = text  // Shows "Hello" on line 1, "World" on line 2

Android's string resource parser interprets \n as a newline character. However, the behavior can be inconsistent in some edge cases.

Method 1: \n in String Resource (Standard)

xml
<!-- strings.xml -->
<string name="address">123 Main Street\nApartment 4B\nNew York, NY 10001</string>
kotlin
1val address = getString(R.string.address)
2textView.text = address
3// 123 Main Street
4// Apartment 4B
5// New York, NY 10001

This is the simplest and most common approach. Android's Resources.getString() converts \n to an actual newline character.

Method 2: Literal Newline in XML

xml
1<!-- strings.xml — actual newline characters -->
2<string name="poem">Roses are red
3Violets are blue
4Sugar is sweet
5And so are you</string>
kotlin
val poem = getString(R.string.poem)
textView.text = poem
// Note: XML may collapse whitespace depending on settings

Actual newlines in the XML source are preserved by Android's resource parser. However, leading/trailing whitespace may be trimmed.

Method 3: CDATA Section

xml
1<!-- strings.xml — CDATA preserves exact formatting -->
2<string name="code_sample"><![CDATA[function hello() {
3    console.log("Hello");
4    return true;
5}]]></string>
kotlin
val code = getString(R.string.code_sample)
textView.text = code
// Preserves all whitespace and newlines exactly

CDATA sections tell the XML parser to treat the content as literal text, preserving all whitespace, newlines, and special characters.

Method 4: HTML Formatting

xml
<!-- strings.xml — use HTML for rich text -->
<string name="formatted"><![CDATA[<b>Bold line</b><br/><i>Italic line</i><br/>Normal line]]></string>
kotlin
1// Must use Html.fromHtml to render HTML tags
2val formatted = getString(R.string.formatted)
3textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
4    Html.fromHtml(formatted, Html.FROM_HTML_MODE_COMPACT)
5} else {
6    @Suppress("DEPRECATION")
7    Html.fromHtml(formatted)
8}
9// Bold line
10// Italic line
11// Normal line

HTML formatting allows <br/> for line breaks plus styling (<b>, <i>, <u>). Wrap the HTML in CDATA to avoid XML parsing issues with angle brackets.

Method 5: String Array for Multiple Lines

xml
1<!-- strings.xml -->
2<string-array name="instructions">
3    <item>Step 1: Open the app</item>
4    <item>Step 2: Tap the button</item>
5    <item>Step 3: Enter your name</item>
6    <item>Step 4: Submit the form</item>
7</string-array>
kotlin
1val instructions = resources.getStringArray(R.array.instructions)
2textView.text = instructions.joinToString("\n")
3// Step 1: Open the app
4// Step 2: Tap the button
5// Step 3: Enter your name
6// Step 4: Submit the form

Programmatic Newlines

kotlin
1// Build multi-line strings in code
2val message = buildString {
3    appendLine("Order Confirmation")
4    appendLine("------------------")
5    appendLine("Item: Widget")
6    appendLine("Quantity: 3")
7    append("Total: $29.97")
8}
9textView.text = message
10
11// Or use string templates
12val name = "Alice"
13val age = 30
14textView.text = "Name: $name\nAge: $age\nCity: New York"

Formatted String Resources with Newlines

xml
<!-- strings.xml -->
<string name="user_info">Name: %1$s\nAge: %2$d\nEmail: %3$s</string>
kotlin
1val info = getString(R.string.user_info, "Alice", 30, "[email protected]")
2textView.text = info
3// Name: Alice
4// Age: 30
5// Email: [email protected]

Format placeholders (%1$s, %2$d) work alongside \n newline escapes in Android string resources.

Common Pitfalls

  • Using \n in XML attributes instead of element text: \n is only interpreted as a newline inside <string> element text. In XML attributes (e.g., android:text="Hello\nWorld" in a layout), \n may be treated as literal backslash-n. Use &#10; for newlines in attributes.
  • Expecting <br/> to work without Html.fromHtml(): If you set textView.text = getString(R.string.html_string), the <br/> tags appear as literal text. You must use Html.fromHtml() to render HTML tags including <br/>.
  • XML whitespace collapsing: XML parsers may collapse multiple whitespace characters (including newlines) into a single space. Use CDATA sections or \n escapes to preserve exact formatting.
  • Forgetting to escape special characters in XML strings: Characters like <, >, &, and ' have special meaning in XML. Use &lt;, &gt;, &amp;, and &apos; or wrap the string in CDATA.
  • Different behavior between getString() and getText(): getString() returns a plain String with \n converted to newlines. getText() returns a CharSequence that preserves HTML-like styling (<b>, <i>) defined in the XML. Use getText() when you need styled text without calling Html.fromHtml().

Summary

  • Use \n in Android <string> resources for newlines — it is converted by the resource parser
  • Use CDATA sections (<![CDATA[...]]>) to preserve exact formatting and special characters
  • Use Html.fromHtml() with <br/> tags for rich text with HTML formatting
  • Use string-array and joinToString("\n") for structured multi-line content
  • getString() converts \n to newlines; getText() preserves inline HTML styling
  • Avoid \n in XML attributes — use &#10; for newlines in attribute values

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.