android itext using assets fonts in Android Studio - android

I'm trying to create Persian pdf files in my android application using www.itextpdf.com but I get java.io.IOException arial.ttf not found as file or resource. Here is the code with the problem.
BaseFont font = BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
this code is fine in eclipse(java project not android) but wont work in Android studio. I don't know how to address the ttf file. Any help would be appreciated.

This is obvious: Android does not have Arial font.
You have to ship the appropriate font for your script (Persian) along with your application and read the font from resources/assets/etc.
See also How to retrieve a list of available/installed fonts in android?.
UPD:
Once you have your file in assets, grab an AssetManager via getAssets() and use it to read the bytes from the font. This answer might be useful.
Afterwards you are free to create your font like this:
BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H, true, false, bytes, null);
Meanwhile, I strongly encourage you to read thoroughly on the licensing of the fonts that are shipped with Windows to determine whether you are able to copy-paste Arial font into your application and then use it like this (I doubt so).

Take a look here:
Attribute
customAttrs:fontType="free_mono"
The TextView
<com.seamusdawkins.fontcustom.CustomTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="#string/str_text_default"
android:textSize="30dp"
customAttrs:fontType="free_mono"
android:gravity="center"/>
See this link for more information.

Related

Android font assets vs font resources (API 26)

In order to use custom fonts in an android app there seem to be two approaches:
Classic way: place TTF or OTF files in the /assets/fonts directory and then build a Typeface with Typeface.createFromAsset(getAssets(), "fonts/custom.ttf").
Natively since API 26, or with AppCompat since API 16: create an XML font family by placing lowercased TTF/OTF files in res/font folder and then reference them directly in XML layouts with android:fontFamily="#font/custom", or access them programatically with ResourcesCompat.getFont(this, R.font.custom).
What are the key differences to keep in mind between font resources and assets?
Specifically, are they rendered in the same way, and is any of them faster or more efficient in terms of performance?
Can it be assumed that font resources are only suitable for fonts that come pre-packaged in the APK, while font assets are more flexible since you can create a Typeface from an arbitrary file inside or outside the APK?
Update:
After a bit of experimentation it looks like font resources are the only way to set custom fonts in AppWidget TextViews without having to manually paint them as bitmaps but that requires the device to actually run API 26 (using the Support Library does not help in this specific case).
Specifically, are they rendered in the same way, and is any of them faster or more efficient in terms of performance?
ResourcesCompat.getFont works like this:
Check in-memory cache if we already resolved a font resource ID to Typeface. If we have a hit, we're done.
Copy the resource to a disk file.
Create Typeface from the file using Typerface.createFromFile and cache it.
That's true for fonts bundled in the APK. I won't go into how downloadable fonts work. You can explore that in the docs or in the source.
Both approaches work the same. They create a Typeface object from a source.
One key difference: If you're directly using Typeface API, you're responsible for caching. You don't want to load the same font multiple times because each Typeface instance takes up loads of memory.
Historically, I was using this code from Calligraphy to take care of caching when loading typefaces from assets.
After a bit of experimentation it looks like font resources are the only way to set custom fonts in AppWidget TextViews [...]
Looks like you're right. Notifications and widgets (anything that uses RemoteViews) can only use natively available resources and attributes on views.
See also: How to use a custom typeface in a widget?

Android font rendering, i am getting very bad view

I am using arial.ttf in my project. In genymotion emulator,in some mobile devices my text view is look good. But in Sony Xperia tablet 4.1.1 i have a text rendering problem. Text looking very bad. What should i do?
<TextView
android:id="#+id/yazi1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Ret"
android:textColor="#color/siyah"
android:textColorHint="#color/siyah"
android:textSize="#dimen/font14" />
Typeface tfArial = Typeface.createFromAsset(getAssets(), "arial_tur2.ttf")
screenshot http://hizliresim.com/ynJjWk
Use OTF instead of TTF. That can be your problem. I was in this situation and changing the file with OTF solved my problem.
OTF is more likely to be a “better” font, as it supports more advanced typesetting features (smallcaps, alternates, ligatures and so on actually inside the font rather than in fiddly separate expert set fonts). It can also contain either spline (TTF-style) or Bezier (PostScript Type 1-style) curves, so hopefully you're getting the shapes the font was originally designed in and not a potentially-poorer-quality conversion.
On the other hand, if you're downloading free fonts from shovelware sites, you're unlikely to get any of that. Indeed, you may simple be getting a TTF font renamed to OTF.

Heavy asterisk (u2731) in Android 2.x

I need Heavy asterisk (u2731) character in my app.
For android 3 and later I used the the following in my TextView:
android:text="\u2731"
and it works fine. But in android 2.x I see a rectangle instead of asterisk. I think that the specific Unicode character is missing in default fonts of Android 2.x .
So what can I do to see this character in Android 2.x? May be I need to load a custom font?
Which font contains this character?
As far as I know, Android 3.0 uses the Roboto font.
You can easily find it for free as a ttf font you might want to store in your assets folder.
Here is the reference and downlad site
[EDIT]
A workaround could be something like this:
String str = "your string containing fat asterisks (**)";
str = str.replace("**", "<b>*</b>");
myTextView.setText(Html.fromHtml(str));
It will make all the ** occurrences become a BOLD *

Changing the android typeface doesn't work

I am using the following line of code to change the font type in android application :L
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/Abumohammed.ttf");
textView.setTypeface(font);
I am sure that Abumohammed.ttf is in assets/fonts folder .. but the font don't change and don't has any effect on the textview !!
Android does not support every font file. When it fails, it tends to fail silently, showing the default font instead. I have no idea what Android does not like about some of them.
I would find some font that definitely works, such as this one, and try it to make sure that the rest of your code is OK. If indeed you determine that the font file does not work, AFAIK you have no choice but to find some other font.
I was annoyed enough by this that I thought I'd share some results of my digging. I tried a few font files that ended up using the fallback font. To 'fix' some of these files, I simply had to open the file in FontForge, then File->Generate Font and re-save as TTF. This allowed some that were not showing to draw properly.
Others required moving the glyphs from the Microsoft area (U+F030) to U+0030 range.
I haven't educated myself on what 'Generate Font' actually does. But, now that I know HOW to fix it, I can at least start to figure out why it isn't working. I suspect this can only be fixed in the AOSP tree however.

Display unicode characters in TextView Android

There are a number of posts all over the internet on this topic, however none of them have been able to isolate and solve the problem.
I am trying to show some special UTF-8 encoded symbols stored in a SQLite database using a TextView, however all it shows is boxes. I understand what this means is that right font is not installed. But when I print those symbols using Arial font on Mac it works.
I am trying to use an Arial typeface on the device and the emulator.
Any advise.
It works on your Mac because the font used by your mac contains those special characters. It would be impossible to create a fontfile that contains all characters defined in unicode. Chinese fonts are a good example: none of them contain all of the ~60.000 known Chinese characters because the font file would be huge and unusable.
You could try using the font from your Mac on android (might be copyright issues - try finding a free font that contains the characters: package the (ttf) file in your application, for example in the /assets folder, and then in your application load that font with
TypeFace typeFace = Typeface.createFromAsset(assetmanager,"fontfile.ttf");
You can then use this in a TextView like so:
TextView view = (TextView) findById(R.id.mytext);
view.setTypeFace(typeFace);
If I'm understanding your situation correctly, you might want to consider the StringEscapeUtils.unescapeXml() method. It's in the common-lang.jar file available here: http://commons.apache.org/lang/
It will take encoded symbols (for example ' for an apostrophe) and get you to a proper character.

Categories

Resources