How can I change the font on android to allow to show special characters like "'" or "à"?
Actually the strings that contains these characters are stored in the sqlite database.
When you load the text into your TextView, will this work for you?
textView.setText(new String(textFromDatabase, "UTF-8"));
This uses the String constructor to set the charset name. You can change "UTF-8" to a different Character encoding -- Also, look at the javadoc for String.
String(byte[] bytes, String charsetName) -
Constructs a new String by decoding the specified array of bytes using the specified charset.
The Droid font supports the "'", "à" and many others characters. I use them all the time (pt language).
Actually, I'm quite sure they support all the Basic Latin, Latin 1 supplement and the first extended latin range. They also support many others like hebrew etc., although I'm not sure if that changed between SDK versions.
You can also download the Unicode Map app in the Market to check which characters are available in your particular device. I also store unicode text in sqlite all the time, and still I don't have any problems.
One thing to consider: check that the encoding you are setting match the encoding of your source. It may be a text or a URL... an example:
BufferedReader b = new BufferedReader(new InputStreamReader(url.openStream(), MY_ENCODING));
Are you sure it's not a problem somewhere?
You should use '' instead of ' to store it into Sqlite database.
For example if you want to store 5 o'clock into database then you have to write this as 5 O''clock. Take a look here, for more information about it.
By default Android SQLite uses UTF-8.
I had this problem because when I populated the database on the first launch I used a txt file with another charset.
Related
i have a simple Memory Game as Project. For the Memory Tiles I wanted to use Emojis. I tried to use it that way:
emojiCard.setText(new String(Character.toChars(Integer.parseInt(1F60D, 16))));
now I just have to save 1F60D to a variable and can show the emoji.
that works for simple emojis but I cannot use the "new" ones because then i have to use surrogate pairs and I don't know how to do this.
Is there a better way ? like saving the unicode ?
sorry i'am really new to android development and tried already a lot of things.
Thanks.
Integer.parseInt() takes a String as input, so presumably you meant to say Integer.parseInt("1F60D", 16) instead. Which would be wasted overhead when you can simply pass a numeric 0x1F60D literal to Character.toChars() instead.
Java strings use UTF-16 encoding. When encoded to UTF-16, codepoint U+1F60D uses surrogate pairs, so surrogates is not your issue.
Assuming you are referring to how newer emojis support modifiers (to change their genders, colors, etc), then that has nothing to do with surrogates. You simply append the modifier codepoint(s) you want after the base emoji codepoint. For example:
emojiCard.setText(new String(Character.toChars(0x1F466)) + new String(Character.toChars(0x1F3FE)));
(👦 + 🏾 = 👦🏾)
I discovered today that Android can't display a small handful of Japanese characters that I'm using in my Japanese-English dictionary app.
The problem comes when I attempt to display the character via TextView.setText(). All of the characters below show up as blank when I attempt to display them in a TextView. It doesn't appear to be an issue with encoding, though - I'm storing the characters in a SQLite database and have verified that Android can understand the characters. Casting the characters to (int) retrieves proper Unicode decimal escapes for all but one of the characters:
String component = cursor.getString(cursor.getColumnIndex("component"));
Log.i("CursorAdapterGridComponents", "Character Code: " + (int) component.charAt(0) + "(" + component + ")");
I had to use Character.codePointAt() to get the decimal escape for the one problematic character:
int codePoint = Character.codePointAt(component, 0);
I don't think I'm doing anything wrong, and as String's are by default UTF-16 encoded, there should be nothing preventing them from displaying the characters.
Below are all of the decimal escapes for the seven problematic characters:
⺅ Character Code: 11909(⺅)
⺌ Character Code: 11916(⺌)
⺾ Character Code: 11966(⺾)
⻏ Character Code: 11983(⻏)
⻖ Character Code: 11990(⻖)
⺹ Character Code: 11961(⺹)
𠆢 Character Code: 131490(𠆢)
Plugging the first six values into http://unicode-table.com/en/ revealed their corresponding Unicode numbers, so I have no doubt that they're valid UTF-8 characters.
The seventh character could only be retrieved from a table of UTF-16 characters: http://www.fileformat.info/info/unicode/char/201a2/browsertest.htm. I could not use its 5-character Unicode number in setText() (as in "\u201a2") because, as I discovered earlier today, Android has no support for Unicode strings past 0xFFFF. As a result, the string was evaluated as "\u201a" + "2". That still doesn't explain why the first six characters won't show up.
What are my options at this point? My first instinct is to just make graphics out of the problematic characters, but Android's highly variable DPI environment makes this a challenging proposition. Is using another font in my app an option? Aside from that, I really have no idea how to proceed.
Is using another font in my app an option?
Sure. Find a font that you are licensed to distribute with your app and has these characters. Package the font in your assets/ directory. Create a Typeface object for that font face. Apply that font to necessary widgets using setTypeface() on TextView.
Here is a sample application demonstrating applying a custom font to a TextView.
Could somebody tell me what is better in terms of performance?
Is it better to save 2 strings at string.xml, like 'abc' and 'abc:'
Or should I save only the first one and concatenate ':' when needed at Java coding ???
Very difficult to answer depending on what your strings will represent and what you need to append. Localization is also an issue, for example...
Dog // English
Chien // French
Hund // German
Using string resources allows you to create different resource files depending on the locale of the device and Android will automatically use the right localized string resource file. If all you need to do is append a single character such as : then you'll double every string for every language.
If you choose to only save the basic strings and append the character using code, then the code will be universal and you'll simply need to append the character to whatever localized word - potentially a lot more efficient.
Both from storage perspective and performance you should save only "abc";
getting extra data from disk takes far longer as some quick in-memory actions.
storing the same data twice is bad practice in general
If you have to concatenate multiple strings you should use StringBuilder - http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html
It's much faster then using '+' or '.concat()'
I am trying to create a database for an android app including, in part, non-English words which require underlines and accents for proper spelling. I set my encoding for this package to utf-8, which allowed the accented characters to store and display properly. However, I cannot seem to get a single character underlined. It displays an empty box for an unrecognized character.
An example of my database helper to create the sqlite is as follows:
cv.put(ENGLISH, "to be alive");
cv.put(NATIVE, "okch_á_a or okchaha");
cv.put(PART_OF_SPEECH, "verb");
cv.put(AUDIO, "alive");
cv.put(VIDEO, "none");
cv.put(IMAGE_DEFAULT, "none");
cv.put(IMAGE_OPTIONAL, "none");
cv.put(IMAGE_TO_USE, "none");
db.insert("words", ENGLISH, cv);
That
_ a _
is the best I can come up with so far, but the a should actually be an underlined character.
I tried html tags like u and /u:
<u>a</u>
since that works with string arrays, but it displays as:
<u>a</u>
(the html is never interpreted).
I tried using:
"\u0332"
as explained at http://www.fileformat.info/info/unicode/char/332/index.htm , but that, too, is never interpreted, so it displays as:
a\u0332
I also tried:
& # 818 ;
and:
& # x332 ;
in a similar manner, with similar lack of results.
Any ideas?
You can store your string in Html format and call .setText(Html.fromHtml(somestring)) from the textview were you want to display it.
I read data from an utf8 database then display it in a listview.
but there are words in the database containing è é à á letters when I get the xml manually in the browser it shows up perfect
but in the listview it shows question marks instead
how can I set the character set to utf8 for my listview?
or is there another way around?
That's really weird, since Android works with UTF-8... In my case I'm using UTF-8 encoded resources (Italian, German, Russian, Chinese), files and so on - and everything works.
There are 3 options why french accented characters shown as "?" marks:
You are during read operation by accident somewhere making implicit translation from UTF-8 to other encoding/Charset
Data stored in database are not really in UTF-8. Sometimes you'd need to use correct SQLite pragma keyword
Your system doesn't have correct font which contains accented characters
You should double check your file encoding on your computer. Does your system use latin-1 (iso-8859-1) or utf-8 ?
Accents can look good in a latin-1 encoded file declaring a xmlns in utf-8 but indeed there not really stored in utf-8.
Salutations,
Stéphane
I had this same problem. Make sure you're not using StringBufferInputStream (which is deprecated). That method causes this issue.
I had this problem once.
You should completely remove your database. I am pretty sure you did the right things to your data with iconv and the pragma in sqlite now. So the problem is that your database in your package is the old version. So clean the project in eclipse, uninstall your app completely from android by removing the database before uninstall, change sqlite installer version code (DatabaseHelper) and relaunch.
Hope it helps,
Stéphane
Use
Html.fromHtml(string)
to display the text correctly.
Let response be the object returned from your server. Do the folling:
response = Utilities.encodedToUTF8(response);
public static String encodedToUTF8(String response) {
try {
response = new String(response.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Error: " + e.getMessage());
}
return response;
}
Since I am developing an application that uses foreign language words (including French) I will share what worked for me. In my application, each character with a special accent was showing as a black diamond with a white question mark. I have a copy of my database on the laptop and use mainly Excel to load data into it (I create CSV files which I then import through DB Browser for SQLite). In my CSV files I could see all the accents correctly, but after I imported them to the database, there were black diamonds everywhere. I tried both encoding, decoding, using Html.fromHtml etc. The only thing that worked for me was to copy a word with special character manually and paste it in the database (with the use of the program). This worked without any problems and now my database contains no black diamonds. The only problem - since it's a manual job it takes time, a lot of time.