How to replace all HTML characters to UTF-8 - android

I'm working on an Android project that reads some XML files and has to send them at some point.
If I have and String like this:
"<frequency_hygiene>about_once_every_2_–_3_weeks</frequency_hygiene>"
Is there any way to replace the HTML "&# 8211;" to its UTF-8 expression "–" dynamically? There can be other HTML codes so a...
mText.replace("&# 8211;", "–")
...it's not enough.
Thanks!
Mauricio

Related

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 can I put utf-16 characters in Android string resource?

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:

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..

writing some characters like '<' in an xml file

since the beginning of my programmation, I used some special character like "<-", ""<<" im my string.xml in Eclipse while developping for Android.
All worked fine for one year, but today, i just wanted to make some minor changes and began to edit my xml files.
I get now compilation error on these characters because eclipse believe it's part of the xml blocks.
Any idea on how I could add this symbol "<" in my xml files?
Thank a lot.
Use
< for <
> for >
& for &
Another way to insert special character follow Moss guide: How can I write character & in android strings.xml by used Unicode definition:
Example:
<string name="item_unknown">\u003c Item Unknown \u003e</string>
which present in string :
< Item Unknown >
I stumbled upon this question, as I use HTML markup in my strings.
If you refer to the Android String Resources documentation,
in the section:
"Styling with HTML markup"
you will see that in strings.xml, you need to use &amplt; for an opening bracket, but you can safely use backslash and a closing bracket. For example:
<b>Text</b>
can be written as:
<b>Text</b>
in your strings.xml.
If using this HTML string in your code, you can use:
Html.fromHtml( getResources().getString(R.string.yourHTMLString )
and the output will be your bold string!

Categories

Resources