Skip to content

Instantly share code, notes, and snippets.

@gmk57
Last active September 19, 2023 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmk57/7b68228ed33ff68a08f6e16c83a4f091 to your computer and use it in GitHub Desktop.
Save gmk57/7b68228ed33ff68a08f6e16c83a4f091 to your computer and use it in GitHub Desktop.
Testing various ways to put line breaks in Android XML resources
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Hello World!"
android:textSize="48sp"
tools:context=".MainActivity" />
private const val TAG = "MainActivity"
class MainActivity : AppCompatActivity(R.layout.activity_main) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
getString(R.string.str10).let {
Log.i(TAG, "str '${it.replace("\n", "\\n")}', raw '$it'")
findViewById<TextView>(R.id.text).apply {
text = it
paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG
}
}
}
}
<resources>
<!-- produces 'A B C' -->
<string name="str1">
A
B
C
</string>
<!-- produces 'A B C' -->
<string name="str2" formatted="true">
A
B
C
</string>
<!-- produces 'A B C' -->
<string name="str3" formatted="false">
A
B
C
</string>
<!-- produces 'A B C' -->
<string name="str4"><![CDATA[
A
B
C
]]></string>
<!-- produces 'A B C' -->
<string name="str5" formatted="true"><![CDATA[
A
B
C
]]></string>
<!-- produces 'A B C' -->
<string name="str6" formatted="false"><![CDATA[
A
B
C
]]></string>
<!-- produces 'A\n B\n C' -->
<string name="str7">
A\n
B\n
C
</string>
<!-- produces 'A \nB \nC' -->
<string name="str8">
A
\nB
\nC
</string>
<!-- produces 'A\n\nB\n\nC\n' -->
<string name="str9">
A\n\
B\n\
C\n\
</string>
<!-- produces 'A\nB\nC' -->
<string name="str10">
A\
B\
C\
</string>
<!-- produces 'A\nB\nC' -->
<string name="str11">
"A
B
C"
</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment