fromHtml: Different behaviour on JellyBean and KitKat (and above) - android

I am using fromHtml to display formatted text (bold italic etc) in TextView. However, I found it's behaviour is different on JellyBean (4.1.2) and KitKat(4.4.2)
Here is code:
String myHtml = "<b>hello</b>😄";
Spanned spanned = Html.fromHtml(myHtml, null, null);
Here html string has 😄 which is unicode for an emoji. Now after calling fromHtml it returns following value on KitKat (and above):
spanned = hello😄
Here is screenshot of Android Studio for the same:
This is expected behaviour as we can see corresponding emoji in spanned.
But on JellyBean the same call returns following value:
spanned = hello��
Here is screenshot:
This is indeed not expected and driving me nuts. I don't know what I am doing wrong. If anyone having idea please can you help?

add this java file in your src and add this font .ttf file in assets dir
now use this like below
String myHtml = "<b>hello</b>😄";
Spanned spanned = AndroidEmoji.ensure(myHtml);
for more check here gitcode.

Surprisingly, root of this problem was in Html.toHtml which I had used to convert text in TextView to html. I used custom toHtmland this problem solved. I used .toHtmlwritten in this answer. Indeed it is great solution. I wonder how Android's original Html.toHtml is so lame and defective.

Related

Using Font Awesome in android app

Im using Font Awesome in my website and I'm I built a website API that my android application suppose to read.
In some labels there are font awesome along with a text.
I successfully implemented Font Awesome in my application but it works for me only when it's hardcoded or as string inside the XML file.
My problem is that I'm reading the font awesome from an API and I'm setting the text with TextBox.setText() method.
I'm facing really strange problem:
holder.gamesTitleTextView.setText("\uf135 " + gamesTitle); // Hardcoded, works
holder.gamesTitleTextView.setText(R.string.fa_icon_rocket + gamesTitle); //From saved string, does not work (returns some numbers)
Saved string is: <string name="fa_icon_rocket">\uf135</string>
Since I have abbility to change my API, if my API contains the font awesome string, \uf135, it shows it as a text and not as font-awesome icon.
Anyone has any way to solve it and use font awesome with the setText() method from a web API?
Please use like below in java code, its working fine for me:
public static final char FA_ICON_ROCKET= "\uf135";

Android Charsequence - replacing text

Due to HTML usage within a string resource, I can't convert this to string from a charsequence (I will lose the formatting otherwise).
<string name="exponent_key">x<sup><small>y</small></sup>/string>
After using getString() I want to replace the 'y' with 'other stuff' but how do you do that? It seems like a simple question but for some reason I can't find anything about it.
Edit: Now that I think about it, can I convert the charsequence to a string that contains the HTML code, and then convert it back to a charsequence later?
Edit: Forgot to mention that the string gets set to a button title, and then retrieved (where it is then used).
There is. Create a function where, as a parameter, you take a string that needs to be formatted. And in function, you just take it through itterator, and after that Html.fromHtml()
in your string.xml
<string name="exponent_key">x<sup><small>%1$d</small></sup></string>
in your code
textView.setText(getString(R.string.exponent_key,2))
Let's break down your question in multiple steps:
Replacing the y with "other stuff" can be done like this:
String.format("%1$", "otherStuff");
If you use getString(), you can do the same thing like that:
<string name="exponent_key">%1$</string>
---
String string = getString(R.string.exponent_key, "otherStuff");
For more than one element, do this way:
you can do that like this:
<string name="string_name">%1$ : %2$</string>
---
getString(R.string.string_name, new String[]{"hello", "world"});
In XML you cannot nest HTML code, since HTML is another type of XML and the parser messes up and cannot recognize what tags are for Android and what are for HTML. But.. there's a trick. You can do that in this way:
<string name="exponent_key"><![CDATA[x<sup><small>%1$</small>/sup>]]></string>
So, with the string above in your XML, just use and you're fine:
getString(R.string.exponent_key, "otherStuff");
Note: if you need to show the HTML in a TextView, just use Html.fromHtml:
textView.setText(Html.fromHtml(getString(R.string.exponent_key, "otherStuff")));
Consider the effect you want to achieve.
you can do like this.
SpannableString ss = new SpannableString("2");
// set superscript
ss.setSpan(new SuperscriptSpan(),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set font size
ss.setSpan(new AbsoluteSizeSpan(12,true),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(ss);

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 Studio messes up brazilian-portuguese characters

My project isn't showing any portuguese characters. When I try to type a word like "Não" it returns Não".
The funny thing is that when I get the string from res/string.xml, it shows the word correctly.
Any idea why?
Things I've tried so far and did not work out:
File -> Settings -> Editor -> File Encondings, I've changed everything to UTF-8 and others, rebuild/cleaned the project, and it kept the same.
EDIT:
I can upload a video on youtube showing it, if it helps with the solution!
There goes an image of what is happening:
My file build.gradle had this line:
compileOptions.encoding = 'ISO-8859-1'
Because of that I wasn't able to change anything. Now it's fixed. :)
This is really hard to explain why is it is, I had same with russian characters, but only on SOME devices. I've just checked to do same as you on Genymotion and it displays correctly... From my investigation it is up to each device how to display given characters, but also I assume it could happen because Android knows how to works with Resources, but doesn't with Strings from code. When you create folders for different languages you don't say that default must be English. So system gonna detect and display. I'm not sure 100%, but this is what I understood from doc.
Anyways, for using String object in TextView from code and displaying foreign (from English) languages we have just 2 options:
1) Add .ttf file for particular text/Unicode
2) html format
Example for first option:
String s="(Mouy t'ngai) (១ ថ្ងៃ)";
TextView text_view1 = null;
text_view1 = (TextView) findViewById(R.id.textView2);
Typeface font= Typeface.createFromAsset(getAssets(), "khmerOS.ttf");
text_view1.setTypeface(font);
text_view1.setText(s);
// you can use different type of .ttf like
TAU_BHON.TTF
molten.ttf
arialuni.ttf
Example of Second option:
tv.setText(Html.fromHtml("\\u27A1");
Source.
P.S. If I missed something, please fill free to notice that.

How to use Swedish character å in string file in android

I have a Swedish word Pågår in my string XML file. When I try to read this word form the string resources it only shows P g r with å character missing in the output. Why is this happening and how can I solve this? Please help me in solving this error.
You can (should) escape special letters with the utf-8 representation for it.
Both \u00e5 and å would work for "å" in Android.
As requested, an example of localized fonts according to link.
To use localized Fonts, you can put the special fonts in assets and name it something like "se-font.ttf"
In your strings.xml for the Swedish language you add a value:
<string name="fontprefix">se-</string>
Then, in your app, when you load the font, you create the asset filename like so:
String fontasset = getString(R.string.fontprefix, "") + "font.ttf";
Then you should be able to load your font using the fontasset filename
in eclipse try to change the xml encoding: - Window->Preferences->XML->XML Files->Encoding
EDIT: And make sure that the font you are using support this character. (just remembered this one - Translate my app to Hungarian )

Categories

Resources