I am getting some JSON data with a formatted string of currency like this
₽35
However, i noticed that on a Nexus 5 (Lollipop) it displays it correctly but other phones such as the HTC one mini and Samsung GT-I9505, it displays a blank character.
I attempted to research the issue, i could not find a solution other than, in the XML layout file, ensure that this line is present
<?xml version="1.0" encoding="utf-8"?>
But i still have the same issue
Please help
Edit 15 May 2015
Loading custom font NotoSans (Please note I know this would leak memory but its just a quick test)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView ruble = (TextView)findViewById(R.id.ruble);
Typeface myFont = Typeface.createFromAsset(getAssets(),"fonts/NotoSans-Regular.ttf");
ruble.setTypeface(myFont);
}
The Russian Ruble symbol defined in strings.xml (note I tried all)
<string name="rubleSymbolJava">\u20BD</string>
<string name="rubleSymbolHTML">₽</string>
<string name="rubleSymbolHTMLHex">₽</string>
Same problem, on older phones shown as a square but works on Android 5.0 BUT nothing older.
SOLVED Please see my answer
I fixed this. I just used the new Google Roboto Font which was last updated Jan 2015 and has the Ruble symbol.
https://www.google.com/fonts/specimen/Roboto#pairings
Research showed that in 2014 Russia changed their currency symbol so old fonts did not have the new Glyph so for others who have a similar issue, see if your font has been updated
The problem-phone's default font might be missing the Russian ruble glyph. Try to hard-code it into a TextView somewhere in your app to make sure that that is indeed the issue rather than some other JSON parsing mistake or so (seems less likely as it works on some phones).
In case the missing glyph is your problem, it's fairly simple to include your own font in your project. That way you can be sure that you can support all glyphs that you need.
Here are the steps to do so programmatically:
Place the .ttf file of your font into the folder assets/fonts in your project
Load it as a TypeFace object via TypeFace myFont = Typeface.createFromAsset(activity.getAssets(), "fonts/font.ttf") (replace myFont and font.ttf as needed)
Assign it to all your TextViews, Paints, etc. via myTextView.setTypeface(myFont). There also seem to be some tricks that allow you to set it as the default font for your entire app.
I haven't used the graphical designer much, but I'm sure it's not any more difficult there either. Also, these instructions are for the Android Eclipse plugin. While the code will likely be identical in Android Studio, I'm not sure that the folder structure is the same. The docs say something about placing the assets in main/assets instead of just assets, for example...
A couple of things to consider:
Make sure that your distribution of the font you choose doesn't violate any licenses of that font.
Your app will now no longer use the device's default font, which will mean that your app will look more similar and predictable across devices, but it might stand out from other apps on specific devices that might use very different default fonts.
The size of the .ttf file will add to the size of your app. Most fonts are fairly small (~100KB), but some do get large (several MB), especially if they support many different languages, which might then become an issue.
Make sure your font supports all glyphs in all languages that your project needs.
An added bonus: You can add some of your own custom glyphs into the font to easily include simple single-colored vector graphics into your app just by putting the corresponding "letter" into a TextView (remember Wingdings?).
I found solution in that answer https://stackoverflow.com/a/36338231/1078641 , added &subset=all parameter to Roboto font link.
The original link to Roboto font was like that
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300italic,300,400italic,500,500italic,700,700italic' rel='stylesheet' type='text/css'>
After I added &subset=all parameter to the link I can successfully see ruble symbol on my Android device
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300italic,300,400italic,500,500italic,700,700italic&subset=all' rel='stylesheet' type='text/css'>
You can also check Android version. I think, some phones on API 19 support russian rubles, but on API 21 they all support it. It would be easier to add string resources instead of check and replace programmatically.
After reading Trying to use <!ENTITY in ANDROID resources with error: "The entity was referenced, but not declared." I made a new file strings.xml in res/values-v21.
So, in values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY rouble "р.">
]>
<resources>
<string name="saaki">Галстук Сааки - %1$s &rouble;</string>
</resources>
In values-v21/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY rouble "₽">
]>
<resources>
<string name="saaki">Галстук Сааки - %1$s &rouble;</string>
</resources>
You can check if the glyph is present using the code from
https://stackoverflow.com/a/29938454/755804
If it's not present, you will need to either provide an image and use TextViewWithImages or replace it with a character sequence.
Related
I am trying to add a custom font following the "Fonts in XML" tutorial on this site. I followed the tutorial to the letter and checked many times to find something I missed but I just can't see it. The font I added is a TTF file.
In the designer, I can select my font family and the text in the TextView changes to the custom font I added. However, when I run the app on my device, the text is defaulted to the regular font. This doesn't happen with the fonts included in Android Studio.
Additional question: when I tried yet another custom font, the text in the TextView changed to the custom font, but the text itself (the content) also changed to some gibberish. Is this an indication of a bad font or something else?
sv_regular.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="#font/sv_font_regular" />
</font-family>
TextView
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="#font/sv_regular"
android:text="#string/app_name" />
You can do this programmatically
Add external fonts in your assets folder
1. Go to the (project folder)
2. Then app>src>main
3. Create folder 'assets>fonts' into the main folder.
4. Put your 'abc.ttf' into the fonts folder.
TextView tx = (TextView)findViewById(R.id.textview1);
Typeface custom_font = Typeface.createFromAsset(getAssets(),"fonts/abc.ttf");
tx.setTypeface(custom_font);
I have encountered such a problem when the font is displayed in Android Studio but does not work on a physical device.
So if you want a custom font from Android Studio to be displayed on your phone screen, you need to add this font to your project as follows:
Open res folder.
Create Android Resource Directory. Resource type - font. You can also name your new folder font.
Then simply drag and drop your ttf font into the font folder. Important: The font name must not contain prohibited characters, and all letters in the name must be lowercase. The name of my font in this example: kopenhagen
(you may have a different font name).
Open your xml file, find the text in which you want to change the font, and just add the following:
android:fontFamily="#font/kopenhagen"
*Instead of the word kopenhagen, use the name of your font
Does your font exactly support the language of the text? This problem occurs when the font does not support certain characters, such as Cyrillic. And one more: try use sv_font_regular in TextView
android:fontFamily="#font/sv_font_regular"
Example
My problem is very simple: I've started an app for playing Darts. The app will have several activities ('pages').
One page will be about the rules of the game. I'll be using a scroll layout because it's quite some text. But how to get the text there?!
I assume working with strings is not the best way? Do I use the XML file to get the text on screen then or does it work via Java (Assetmanager)?
Maybe there are sample apps in which large chunks of text are used?
I know this really might seem like a trivial question but I haven't a clue where to begin.
Thanks in advance!
You should put your string in your strings.xml in your res\values folder.
You can define strings by ID which allows easier internationalization (i18n), so that you can easily adjust the strings used in your app to locale (which is done automatically using resource identifiers, and it falls back to strings.xml if it can't find a strings-hu.xml in case you have Hungarian locale set as system language).
You can also define string-array and the like in XMLs. Then all you need is create a layout XML with a ScrollView in it that has a TextView in it and then you set android:text="#string/rules" for that TextView and you're done.
It is so simple my friend.
You can simply use TextView and in "android:text" you refer to the string that you delared in strings.xml file (by its name)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/text_name"
/>
If your text is dynamic, you can modify it in Java code!
Make a String Resource like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1"> your text </string>
<string name="string2"> your text </string>
</resources>
and access like this if you are in Activity.
getResources().getString(R.string.string1);
If you are a begginer you should read some tutorials after post a question...
I give you a three nice tutorials below :
Want to Learn How to Program for Android? Start Here
Android Programming Tutorial
Android Development with Android Studio or Eclipse ADT
About your question, if you don't know how to use the string.xml resource just read the string-resource guide
Hope it helps.
Background
Lint has a relatively new feature, so that it will warn us about missing translation only for languages that we choose, but i don't get how to use it.
The problem
for some reason, Lint still warns me about languages that i don't intend on translating yet.
What i've tried
for example, currently i want to only have 2 languages : english ("en") and hebrew (which is sadly both "iw" and "he" ) .
so i have strings files in the folders :
values (for english)
"values-he" and "values-iw" (for hebrew) .
i've tried putting the new attribute in the english file as such :
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en,iw,he">
...
The question
what is the right way to do it?
Looking here it seems that it's to be used into resource files to indicate the default language. So you can specify only one locale code.
should correspond to a language
Moreover it seems to be used only to disable spell-checker
If you read the article:
This lets you tell the tools which language you're using in your
base values folder. For strings in for example values-de or values-en it's obvious, but not in the base "values" folder
It need only to know what is the language in the default "values" folder (the folder without any attribute).
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en">
You are already in right direction. Just need some modification. Like this manner:
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">
Now we know that the language used for strings in the default values folder is Spanish rather than English.
Used by: Lint, Studio (to disable spell checking in non-English resource files)
Reference Link: Go to here tools:locale
Thanks.
I have some text which has some bolded parts. Until KitKat this strategy (as mentioned in this post) worked perfectly well
My strings resources file:
<string name="multi_style_text">NON-BOLD TEXT \n<b>BOLD</b></string>
My application code in fragment:
txtView.setTypeface(FontUtils.getOstrichRegular(this.getActivity()));
...
public static Typeface getOstrichRegular(Context context) {
return Typeface.createFromAsset(context.getAssets(),
"fonts/ostrich_regular.ttf");
}
Currently (in KitKat), the bolded part is not shown in the custom font, the non-bolded part is shown in the custom font. In previous versions of Android, all of the text was shown in the custom font.
What gives?
So, after being frustrated by this bug, I searched around and found a solution to the problem.
In my current project we use calibri.ttf font. that was working fine up to 4.4. Once i got the update to my nexus 4, All the TextViews with Calibri font were showing "ff" instead of the entire text.
THE FIX - get an .otf (open type font) version of your font, and put in the project, works like a charm. Too bad google didn't inform the developers on this and there's very little documentation on the matter.
Apparently this is a bug in KitKat and has been fixed in an internal tree.
Put your custom font in android assets under folder name "font" or whatever you want
Try this
myTypeface = Typeface.createFromAsset(this.getAssets(),
"fonts/<<your font>>.ttf");
in onCreate() then
[use youcontroll].setTypeface(myTypeface);
Best of Luck...
I resolved the problem by converting my file.ttf to file.otf
remplace :
Typeface typeface = Typeface.createFromAsset(activity.getAssets(), "fonts/ostrich_regular.ttf");
yourTextView.setTypeface(typeface);
by :
Typeface typeface = Typeface.createFromAsset(activity.getAssets(), "fonts/ostrich_regular.otf");
yourTextView.setTypeface(typeface);
FYI : the .otf format work for all android version (not only on kitkat)
After many hours searching for roboto.otf (2014 year) I understood that it was a mistake. Simply download a normal ttf font from https://www.fontsquirrel.com/fonts/roboto-2014 and copy to assets folder, then use setTypeface.
Also you may convert it to otf with any web-site.
I have the following resource in my strings.xml
<string name="punjabi">ਪੰਜਾਬੀ</string>
This gets displayed in the Android string resource editor and my application just fine.
However in the eclipse layout editor, the characters display as boxes, similar to □□□□□□
The box characters indicate a font glyph look up failure.
Any ideas on how to get Eclipse to display unicode in the layout designer?
There are two things you need. The font itself, and the fallback mapping.
First, copy the font needed, such as Lohit-Punjabi.ttf into the platform specific fonts directory:
sdk\platforms\android-###\data\fonts
Second, add the fallback font into the font definition file in that directory:
For android SDK version up to 13 add the following line in fonts.xml
<fallback ttf="Lohit-Punjabi" />
For android SDK version 14 and up add the following line in fallback_fonts.xml
<family>
<fileset>
<file>Lohit-Punjabi.ttf</file>
</fileset>
</family>