When displaying japanese text in a textview, android defaults to using a chinese font, showing the wrong characters, exemplified here. Setting the locale to japanese works on the emulator, but doesn't work on my galaxy s3, probably because it doesn't support Japanese. There other solution is to set the font programattically from an asset such as:
textView.setTypeface(Typeface.createFromAsset(getAssets(), "DroidSansJapanese.ttf"));
but that seems like a bit of a kludge. Is there any way to set the font through the xml layout to one that supports japanese?
Generally, older (<4.1) Android devices sold outside of Japan render the Chinese version of kanji by default. Android devices sold inside Japan render the Japanese version of kanji by default. DroidSansJapanese is the Japanese font counter-part to DroidSans and is available in AOSP. It seems like a manufacturer can choose the preference in which system/vendor-provided fonts are used when displaying text, at the time of creating their build of Android. Even if you change your locale on a (<4.1) non-Japanese device, it can still display the incorrect version of the characters.
If you want to localize your app for Japanese users in Japan, they most likely will have a device purchased in Japan and so kanji will display correctly (assuming the manufacturer took the required above action). If this behavior is acceptable to you, then you need not make any changes. If you are building an app targeting users outside of Japan (e.g. people wishing to study Japanese), then this may not sound so good.
Support for Japanese-style kanji improved when Android 4.1 was released. This commit has some detail on what was changed. If your phone is currently using the Japanese locale, Android should display Japanese-style kanji. I have seen this behavior in action with an non-Japanese Samsung Galaxy Note 2 running Android 4.1. When I set my system language to English (UK/US), I see the Chinese-style rendering when looking at kanji text. However, when I set the language to 日本語, I can see the Japanese-style rendering. I have not tested this, but I am wondering if forcing the locale programmatically can also force the display of Japanese-style kanji for 4.1+ devices.
You can't set your font to one you have in the assets via an XML attribute. You have to do this in Java using code similar to what you posted in the original question. To do this globally, you can either make your own subclass of TextView that sets the right font, or you can iterate through all of the views in your layout (e.g. after Activity.setContentView(int)) and update them accordingly if they are TextViews. There is a somewhat more detailed description of this solution in another question. Including a Japanese font will probably bloat your APK by a sizable amount.
If you are writing an app for Japanese users in Japan, I would not bother taking any action since it should already display correctly for them. Users of Android 4.1+ devices also have a good experience, provided they are using the Japanese locale. Otherwise, bundling a font is your answer, and you already have a good idea of what is required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With