Sunday, January 20, 2013

Displaying Unicode Symbols in Android

Eventually in Android, you will need to display a non-ASCII character. You know, characters like the cents or plus minus symbols. They exist in Unicode, but not normally on American keyboards.  And surprisingly there isn't much information on how to do it.

Since I also do web development, I know that HTML has support for the full gallery of Unicode symbols. In order to display the cents symbol, the HTML is: &cents; and for the plus minus symbol it is: ±

Android has a class, Html, which has static methods for converting strings to and from HTML. For us, the method of interest is: fromHtml. This method: returns displayable styled text from the provided HTML string. Here is how we use it:


static String plusMinusSymbol = Html.fromHtml("±").toString();

Now, plusMinusSymbol, holds the string for the symbol, it can be used any where a String is expected. You can use the HTML for any Unicode symbol.