Using Font Awesome in android app - android

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";

Related

Marathi font not rendered correctly?

I am creating an android mobile app, which takes data from user and saves to database. Now while generating report I am creating an pdf dynamically using iText pdf creator of version iTextg 5.5.10. I want to show text like this "मासिक दैनंदिनी"but it shows like this "मासकि दैनंदनिी". Also joined words looks like this "कोलहापुर" which should be "कोल्हापुर. We are using "NotoSansDevanagari-Regular.ttf" fontfor marathi font.
We have tried couple of things listed below,
1. We tried pdf box pdf creator but having same issue.
2. Tried different fonts like mangal.ttf, krutidev.ttf.
3. tried unicode fonts using jar files.
I am creating custom font object like this,
BaseFont bf_mar = BaseFont.createFont("assets/NotoSansDevanagari-
Regular.ttf", BaseFont.IDENTITY_H,true);
Font marathi = new Font(bf_mar , 20,Font.NORMAL);
And applying the font to paragraph like this,
Paragraph paragraph = new Paragraph("मासिक दैनंदिनी",marathi);
paragraph.setSpacingAfter(5);
paragraph.setSpacingBefore(5);
document.add(paragraph);
I expect the text should appear like this "मासिक दैनंदिनी". Also joined words looks like this "कोलहापुर" which should be "कोल्हापुर
Try without the use of fonts if it is not compulsory to use the exact font in the report.

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

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.

Android studio how to save characters č,ć,đ,ž in string?

Generally, I'm trying to save some text from edittext in android studio to string and than later write it to pdf using pdf stamper and send it via e-mail. I'm using default encoding UTF-8 because I cannot (or don't know) to change it since it's hard-coded. User will eventually enter some of these characters. What is the best way to make this characters visible or to prevent user from typing those characters?
Thanks :)
Use special characters and set it to text view like how u set & :- &
or use Html within TextView :-
#For example (< Android Nougat):
myTextView.setText(Html.fromHtml("č"));
#For example (>= Android Nougat):
myTextView.setText(Html.fromHtml("č", Html.FROM_HTML_MODE_COMPACT));
try this,
myTextView.setText(Html.fromHtml("č"));
You can save it to xml as
<string name="random">" č,ć,đ,ž"</string>
and you it later by string name

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.

Display ® registered trademark symbol in Android v4.0 WebView?

The source HTML string (including the symbol) is coming from the strings.xml resource file, and is destined to be displayed in a WebView. I've tested with this in the resources:
<string name="MY_STRING">®</string>
Using the actual trademark symbol in the resources (®), the projects builds, but when displayed in the WebView it shows as "®" (i.e. an "A" circumflex, followed by the registered trademark symbol) - i.e. two characters are shown, the first incorrect & unwanted.
I see the same result when using the entity reference, ®
Using ® fails, and the project does not build.
This is the code that pushes the string resource into the WebView:
String html = getString(R.string.MY_STRING);
((WebView)findViewById(R.id.terms_web_view)).loadData(html, "text/html", "UTF-8");
I also tried this, but it did not help:
webView.getSettings().setDefaultTextEncodingName("UTF-8");
I pushed the HTML string to Logcat, and it looks fine - it shows the symbol correctly. So if the string is ok, and the WebView is set to use UTF-8, why is the symbol not displaying correctly?
UPDATE
I tested on other devices. I can only reproduce this issue on a Galaxy Nexus on Android v4.04. On a Nexus One v2.3.x, Wildfire S on v2.3.x and a Samsung Tab 10.1 on v3.2, it works fine. I've changed the question title to clarify this is an ICS issue.
String resources are not designed to hold arbitrary HTML, including arbitrary entity references.
You might be able to get an arbitrary entity reference to work if you pre-escape it:
<string name="MY_STRING">&reg;</string>
IIRC, that should decode to ® after your call to getString().
At the end of the day, you need to get ® to WebView. If you cannot determine a way to do that with a string resource, you will need to store this value someplace else.
You are using loadData(html, "text/html", "UTF-8"); This method expect a html string in your variable html. But it is not. Try
String html = "<html><body>My text is ®</body></html>";
for instance.
--updated to have a full html document
And if you store it in a resource file use :
<string name="MY_STRING" formatted="false"><html><body>My text is ®</body></html></string>
This is not and Android developer answer but just something simple if you're trying to text somebody and neither Ⓡ nor ™ aren't available as symbols (like on my LG phone Android 7.0). Just copy those symbols from this message thread and send them in a message to yourself or stuff them in a quick memo. Then you have them readily available for future use.

Categories

Resources