As a note to self, when you’re writing an Android application and you think you want to store some static text in an external file, a better approach can be to create a resource file under res/values.
For example, I’m currently adding some help text to an Android app, and to do that I created a file named strings_help.xml under the res/values directory. That file contains HTML wrapped in an XML CDATA tag, like this:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="help_string"> <![CDATA[ <!-- NOTE: i changed all single-percent symbols to double-percent symbols b/c they were causing problems --> <!-- SEE: http://developer.android.com/guide/topics/resources/string-resource.html --> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>your title</title> <style> <!-- style stuff in here ... --> </style> </head> <body> <p>Help text in here ... </body> </html> ]]> </string> </resources>
Once you’ve created that file, you can access it in your Android/Java code like this:
Note that the string R.string.help_string must match the name you gave the text in the XML file. (To be clear, this name does not match the XML filename.)
On a related note, here’s a link to another example of how to put plain static text in an Android strings/resources file.
Bonus: How to show static HTML in an Android WebView
As a bonus note, if you happen to want to display that static text in an Android
WebView, you can do that like this:
WebView webView = new WebView(context); String helpText = context.getString(R.string.help_string); webView.loadData(helpText, "text/html", "utf-8"); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Help") .setView(webView) .setNeutralButton("OK", null) .show();
In summary, if you wanted to see how to store some static text somewhere (other than a file) in an Android application, I hope this example is helpful.