Wednesday, August 22, 2012

Android TextView accented characters

I have a list of addresses for various places in Europe that I am incorporating into a Monodroid application and have had little success in getting TextView to display characters with accents.

I saved the file in UTF-8 and in Visual Studio, the file opens fine and the accented characters display correctly. However, opening the file from assets or from an embedded resource stream and then showing it as text in a TextView was driving me mad because it refused to show accented characters in the final screen.

Here's how I fixed it:

#1 Ensure that the values are saved as UNICODE. Do this by opening the file in Notepad and saving plain text with Unicode encoding. Even though your file looks like its encoded as UTF-8 the Android device won't like it.

#2 Embed this file into the assembly as an EMBEDDED RESOURCE

#3 Load the file using GetManifestResourceStream

#4 use a StreamReader to wrap the resource stream and declare new StreamReader(theStream, Encoding.Unicode);

The standard Android fonts all handle unicode character sets.


No comments: