How can I put utf-16 characters in Android string resource? - android

I want to use Emojis in my app's strings. All strings reside, of course, in strings.xml
The problem is that not all Emojis are 16 bit friendly. Some Emojis can be represented as "normal" 16 bit hex: '\u26FF' but some are 32 bit hexes (UTF-16), usually represented as: '\x1F600'. I have no problem dealing with those inside the app, in code. But the strings.xml resource file is UTF8 encoded, and does not deal properly with non 16 bit escape chars.
I tried using '\x1F600' - because I saw that '\u26FF' works just fine. But it seems not to devour the 'x' escape char. Nor did it like the regexp notation '\x{1F600}'
So I ended up using a string placeholder '%1$s' and filling in the Emoji in code like this:
// greeting_3 is defined as: "hello there %1$s!"
String s = context.getString(R.string.greeting_3, "😜");
// OR:
String s = context.getString(R.string.greeting_3, new String(Character.toChars(0x1F61C)));
This is not a very elegant solution... is there a proper way to put 32 bit UTF-8 chars in strings.xml ?

But the strings.xml resource file is UTF8
If it's UTF-8 encoded, you can put your emojis directly. But then you risk that your editor or another piece of software destroys them.
If you are putting them in XML, you can try using XML entities: 😀, I'm not sure how well Android supports them though.
You can also use surrogate pairs: convert the emoji to UTF-16 and use standard \u escape. You can for example check out this page, it even tells you how to create a string litaral in Java: http://www.fileformat.info/info/unicode/char/1F600/index.htm
😜 → U+1F600 → "\uD83D\uDE00"

The easiest way it just copying and pasting the emoji, it works from Android Studio 3.0 and newer

Add the resource like follows:
<string name="string_title">This is a emoji example <U+1F642></string>
In Android Studio 3.0 you can copy and paste an emoji:
And here how it looks:

Related

Best Way to store and render Unicode Emojis in Android

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)));
(👦 + 🏾 = 👦🏾)

display special character correctly

I have got the problem, that I can't display special characters like 'ß, ö, ä, ü' in my app.
The result is like this:
Or like this:
In Android Studio I get no error or warning and I am using the windows-1251 encoding.
In gradle I added this line: compileOptions.encoding = 'windows-1251'
It shall be german encoding.
How to fix that?
I actually can change special characters like 'ß' from predefined Strings with Html.fromHtml( unicode of the special character) like Html.fromHtml ( "&#223" )
But what if a non predefined String contains a special Character? I have tried to simple replace the special character like this:
String neuer_String = get_street_adress.replace ( "ß", Html.fromHtml ( "&#223" ) ) ;
straße.setText ( "Stra" + Html.fromHtml ( "&#223" ) + "e: " + neuer_String);
But this doesn't work, because the string FriedhofstraÃe for example should be Friedhofstraße. So there is no character 'ß' in the string.
I also tried to change the font with Typeface but a exception is thrown: "native typeface cannot be made"
It's probably 'cause of the font in your device.
The way you can resolve this is by coding a Typeface out of a working font that you have tested.(e.g in Word,etc)
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/theNameOfTheFont.ttf");
Remember to place the code in fonts folder in assets. If you don't have one, create it.
Using UTF-8 encoding "might" solve your problem.
It's strongly recommended to use UTF-8 encoding.
With UTF-8 encoding, you can easily use the unicode value in your string.xml.
Here the a Link to XML character Entities List.
For example for ß use <string name="text szlig">text ß</string>
Go to the settings of Android Studio. In the File Encoding tab, make everything be formatted as UTF-8.
If the problem still persists, then the problem is in the font you are using as it may not support special characters.
I have found a solution for your problem, check it:
You can save all your strings with special characters in your strings.xml like:
<resources>
<string name="street">Straße</string>
</resources>
Then retrieve them in code like:
String street = getResources().getString(R.string.street);
You can definitely try other german characters out. Hope that works for you!

Android XML Editor removes Unicode Escapes

Hi i have unicode escapes in my xml files like that
android:text=""
but after hitting strg+shift+f for formatting it is replaced by
android:text=""
can i do anything about that?
For java recommended is \uF100 instead of  - this is not displayed even in my browser if not as "code" on fileinfo. Moreover, onfileino they posted:
U+F100 is not a valid unicode character.
But back to question - are you sure your file is utf-8 and project is also?

How to put Hebrew character in Android JAVA file?

For Android Platform:
I need to put Hebrew Character ₪ and some more like אורנג in string to check with the incoming data in java file. When I put this character It shows an error like "Some characters can not be mapped using "Cp1252" character encoding. Either change the encoding or remove the characters which are not supported by the "Cp1252" character encoding". These values are coming from SQLite database. Please see the attached snap. How can I solve this? Kindly give me some useful suggestions. Looking forward to hear from anybody who has a suggestion for me. Thanks.
Please open your eclipse.ini file from your eclipse folder with Note Pad and put the following permission into that.
-Dfile.encoding = UTF-8
Some more options as well:
For setting the encoding on a per WorkSpace basis, use Preferences->General->Workspace
To set the encoding on a per project basis open project properties and change to UTF-8
Hope this one help.
I think you'll need UTF-8 encoding for that.
These things are a little bit confusing. Reading this might clear some of the mist around encodings and character sets.
I think this may useful to you.
Keep this hebrew word in string.xml file. You can retrieve as
in String.xml
place hebrew word here
In java code:
String s=getString(R.string.hebrew_word);
Use this string as you want..

Problem with special characters

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.

Categories

Resources