Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display unicode characters in TextView Android

Tags:

android

There are a number of posts all over the internet on this topic, however none of them have been able to isolate and solve the problem.

I am trying to show some special UTF-8 encoded symbols stored in a SQLite database using a TextView, however all it shows is boxes. I understand what this means is that right font is not installed. But when I print those symbols using Arial font on Mac it works.

I am trying to use an Arial typeface on the device and the emulator.

Any advise.

like image 275
user210504 Avatar asked Oct 26 '10 02:10

user210504


2 Answers

It works on your Mac because the font used by your mac contains those special characters. It would be impossible to create a fontfile that contains all characters defined in unicode. Chinese fonts are a good example: none of them contain all of the ~60.000 known Chinese characters because the font file would be huge and unusable.

You could try using the font from your Mac on android (might be copyright issues - try finding a free font that contains the characters: package the (ttf) file in your application, for example in the /assets folder, and then in your application load that font with

TypeFace typeFace = Typeface.createFromAsset(assetmanager,"fontfile.ttf");

You can then use this in a TextView like so:

TextView view = (TextView) findById(R.id.mytext);

view.setTypeFace(typeFace);

like image 124
edovino Avatar answered Sep 21 '22 18:09

edovino


If I'm understanding your situation correctly, you might want to consider the StringEscapeUtils.unescapeXml() method. It's in the common-lang.jar file available here: http://commons.apache.org/lang/

It will take encoded symbols (for example ' for an apostrophe) and get you to a proper character.

like image 43
Dave MacLean Avatar answered Sep 18 '22 18:09

Dave MacLean