Using a Format String

The last preliminary is to create a template crime report that can be configured with the specific crime’s details. Because you will not know a crime’s details until runtime, you must use a format string with placeholders that can be replaced at runtime. Here is the format string you will use:

<​s​t​r​i​n​g​ ​n​a​m​e​=​"​c​r​i​m​e​_​r​e​p​o​r​t​"​>​%​1​$​s​!​ ​T​h​e​ ​c​r​i​m​e​ ​w​a​s​ ​d​i​s​c​o​v​e​r​e​d​ ​o​n​ ​%​2​$​s​.​ ​%​3​$​s​,​ ​a​n​d​ ​%​4​$​s​

The %1$s, %2$s, etc. are placeholders that expect string arguments. In code, you will call getString(…) and pass in the format string and four other strings in the order in which they should replace the placeholders.

First, in strings.xml, add the strings shown ...

Get Android Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.