How to replace special characters with their equivalent in android? - android

While i'm retrieving data from remote server i'm getting some special
characters when i get known special characters i replaced them
with their original characters but when i don't no the
special characters how can i replace them? Some of the special
characters are as follows..
â €œ €™ € €“ â; ' & € ü Ü Û Ù
û ù Ø ß
Above are some them like this i'm getting some other . How to replace
these with their original characters in android.

It seems your question is how do you decode html encoded text?
Html.fromHtml(server_response).toString();

Html.fromHtml(server_response).toString();

Related

What's the difference between Android's Html.escapeHtml and TextUtils.htmlEncode ? When should I use one or the other?

Android has two different ways to escape / encode HTML characters / entities in Strings:
Html.escapeHtml(String), added in API 16 (Android 4.1). The docs say:
Returns an HTML escaped representation of the given plain text.
TextUtils.htmlEncode(String) For this one, the docs say:
Html-encode the string.
Reading the docs, they both seem to do pretty much the same thing, but, when testing them, I get some pretty mysterious (to me) output.
Eg. With the input: <p>This is a quote ". This is a euro symbol: €. <b>This is some bold text</b></p>
Html.escapeHtml gives:
<p>This is a quote ". This is a euro symbol: €. <b>This is some bold text</b></p>
Whereas TextUtils.htmlEncode gives:
<p>This is a quote ". This is a euro symbol: €. <b>This is some bold text</b></p>
So it seems that the second escapes / encodes the quote ("), but the first doesn't, although the first encodes the Euro symbol, but the second doesn't. I'm confused.
So what's the difference between these two methods ? Which characters does each escape / encode ? What's the difference between encoding and escaping here ? When should I use one or the other (or should I, gasp, use them both together ?) ?
You can compare their sources:
This is what Html.escapeHtml uses underneath:
https://github.com/android/platform_frameworks_base/blob/d59921149bb5948ffbcb9a9e832e9ac1538e05a0/core/java/android/text/Html.java#L387
This is TextUtils.htmlEncode:
https://github.com/android/platform_frameworks_base/blob/d59921149bb5948ffbcb9a9e832e9ac1538e05a0/core/java/android/text/TextUtils.java#L1361
As you can see, the latter only quotes certain characters that are reserved for markup in HTML, while the former also encodes non-ASCII characters, so they can be represented in ASCII.
Thus, if your input only contains Latin characters (which is usually unlikely nowadays), or you have set up Unicode in your HTML page properly, and can go along with TextUtils.htmlEncode. Whereas if you need to ensure that your text works even if transmitted via 7-bit channels, use Html.escapeHtml.
As for the different treating of the quote character (") -- it only needs to be escaped inside attribute values (see the spec), so if you are not putting your text there, you should be fine.
Thus, my personal choice would be Html.escapeHtml, as it seems to be more versatile.

Adding symbols through strings.xml file

I am displaying Activity's Title from strings.xml file like this
setTitle(R.string.add_contact_title);
I have following string in strings.xml
<string name="add_contact_title">Anadir Contacto</string>
but i want to display title like Añadir Contacto instead of Anadir Contacto.
is there any way to do this within the strings.xml file rather than copying and pasting the characters from another source?
You can also use unicode using the \u escape sequence like this:
<string name="add_contact_title">A\u00F1adir Contacto</string>
See here for a list of common unicode latin characters: http://unicode-table.com/en/#latin-extended-b
You can use ñ
You can find all the HTML Special Characters in this page
Special Characters in Android & HTML
just replace the code where you want to put that character. :-)
Yes you can do that. You have to start the special character with &amp
i.e for your case it will be like ñ
<string name="add_contact_title">Añadir Contacto</string>
Just check the link which will convert ASCII into Unicode
ASCII to Unicode
and Unicode Reference

What are my options for displaying characters that Android can't?

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.

get rid of q‌u‌e‌s‌t‌i‌o‌n mark in textview

I'm reading a file into a jsonobject from my assets folder. The file contains json string.
Some of the strings contain "'" (apsotrophe) character. The problem is that the textview shows "?" in place of these apostrophes. Why is this happening. When I print the json string to logcat using mJsonObject.toString(), it shows proper character.
How can I get rid of this "?" and show actual character?
The Apostrophe probably isn't a simple ' apostrophe, but some advanced typographic apostrophe that is missing in your font and/or gets mangled during charset conversions. Preferably, replace the typographic apostrophes with plain apostrophes in the JSON file.
If you don't want to do so, escape them using the \u escape. This makes sure that the correct character ends up in the JsonObject. If you still get the question mark, make sure your font supports the character and that you don't break it in other charset conversions.
If you cannot use \u escapes for some reason, make sure you read the file with the correct charset.

I want to search Ö this character in list

Is it possible to check out the android api level in html css?
you can use unicode character for your problem
here is the table for some character with equivalent unicode character.
Ä, ä \u00c4, \u00e4
Ö, ö \u00d6, \u00f6
Ü, ü \u00dc, \u00fc
ß \u00df

Categories

Resources