According to the Android Typography page Roboto font was introduced in Ice Cream Sandwich. I can download the .ttf files there or I can find it (and many others) in the <android-sdk>/platforms/android-x/data/fonts directory (where x is ICS version and higher).
If I want to actually use this font in my app do I still need to copy this to my assets/fonts directory and setup the font like this or is there some other way of accessing it in my layout XML files?
EDIT:
To clarify the question I really mean any of the Android supplied fonts. So if you look in Android Lollipop's font folder (android-sdk/platforms/android-21/data) there are lots of new fonts.
Assume on a single layout I want to use Roboto-Italic in one TextView and (say) NotoSerif-Bold in another. Can I specify that in my XML layout file using android:typeface="..." or do I need to manually copy the required .ttf files to my font folder and subclass the TextView widget?
Hi you would need to set the typeface on a TextView like below:
Typeface yourTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName + ".ttf");
yourTextView.setTypeface(yourTypeface);
The Roboto font is already used by default on android 4.0 and up, so you don't need to do anything.
If you want set Roboto as your font, you must verify what is your version, if you have Android < 4.0 you must do something like this
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/roboto.ttf");
TextView text = (TextView) findViewById(R.id.text);
text.setTypeface(font);
But if your version is 4.0 or higher as JonasCz says, is not necessary, you can read more about this in https://developer.android.com/design/style/typography.html
Note: copy the fonts to assets/fonts. This code is useful also to put any font in your views.
Related
I added a Google font using Android Studio,
It created a 'font' folder and under it is an XML file as follow:
res -> font -> font_name.xml
I'm trying to apply it programmatically but I can'y find how to do it,
I've tried several codes but nothing worked, examples:
Typeface font = Typeface.createFromFile("font/font_name.xml");
or
Typeface font = Typeface.createFromAsset(getAssets(), "#font/font_name");
Please note,
The 'font' folder along with the 'font_name.xml' file,
Were created automatically by Android Studio.
Your help would be appreciated,
Thank you.
I am certainly not an expert, but I managed to get my Typefaces working.
Firstly, this thread contains a lot of useful info:
Custom fonts and XML layouts (Android)
Now specifically to your problem, the way I managed to get Typefaces to work is by downloading a .ttf file either somewhere online, or when using android studio and clicking more fonts, set the radio button to add font to project. That will download the .ttf to res\font\font.ttf Move it to your assets directory into fonts folder and then create the typeface like this:
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");
If you want to use your font often and don't want to bother setting it every time, a good idea might be extending a TextView or whatever class you need and using the .setTypeface() method in the custom constructor.
Just solved this myself, here is what I have put together.
Typeface font = Typeface.create("cursive", Typeface.NORMAL);
Log.e("Setup", "Font: " + font.getStyle());
signatureButton.setTypeface(font);
The log just checks if you have selected an acceptable font, it should return 0 if not. The hardest part was finding a font family that worked. The key is to go to your XML file and type "android:fontFamily=" from here a suggestion list should pop up with choices at the bottom.
To change the google font family
Typeface typeface = ResourcesCompat.getFont(this, R.font.your_font);
textView.setTypeface(typeface);
Add that to your fonts folder then use them in the XML or programmatically.
I have a TextView which contains just one letter. The size of the text is calibrated so that the letter occupies entire TextView area. Now one of my users reported a problem that the letter does not fit properly into the TextView. From the attached screenshot I can see that she uses some kind of custom font on her Samsung S4 device. I am sure that's the problem. Here are some snapshots:
Custom font in a TextView over ImageView:
Custom font in the Status Bar:
Distorted letter N in my TextView:
Is there any way to make a TextView use standard android font ignoring any custom fonts that users applies? How can I install custom fonts on the emulator or Sony phone (do not have Samsung) and replicate the behavior?
Yes you can set custom fonts to your app. Make use TypeFace class.
Create a folder called fonts under assets folder and place all your fonts in it (Font format ttc or ttf. Folder name can be anything)
Within you onCreate method add the below code.
TextView mytextview= (TextView) findViewById(R.id.text_view_id);
// Loading Font Face. Replace with your font file name
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/your_custom_font.ttf");
// Applying font
mytextview.setTypeface(tf);
Similar way you can have as many as TypeFace you require and also use the same TypeFace declared on as many as TextView you want.
This tutorial below very well explains your requirement and also provides screenshot for better assistance.
http://www.androidhive.info/2012/02/android-using-external-fonts/
Also kindly note that TypeFace has a lot of other options that you can play around with. You can refer the android docs to get an idea about them.
I hope it helped you.
Thanks!!
My advise:
Create a subfolder in your asset folder called fonts.
Put there the fonts you want to use (for example ttc, or ttf) Set
your TextView typeface. For example:
yourTextView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/yourFont.ttc"));
I hope it helps!!
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 am developing an Android aap that uses Arabic Fonts.
I have successfully display Arabic text by using "tahoma.ttf"
But problem is that these Arabic Fonts Didn't give a cool look.
So i wanted to change that Font Style. Is there any way of doing this ?
If you want to use external font do :
First step is to pick a font that you want to use.
Create a fonts folder in your assets directory and copy your font there.
To access your custom font,use the Typeface class in the Android SDK to create a typeface that Android can use, then set any display elements that need to use your custom font appropriately.
Unfortunately, you can no longer use layout XML for this, since the XML does not know about any fonts you may have tucked away as an application asset.You can do like this in your code:
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/BPreplay.otf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);
Note: Android does not seem to like all TrueType fonts.So, if you try to use a different font and it does not seem to be working, it may be that the font is incompatible with Android, for whatever reason.
You can see more details in these pages:
Fun with Fonts
Musings of the Bare Bones Coder/using custom fonts
I would like to have an app include a custom font for rendering text, load it, and then use it with standard elements like StaticText. Is this possible?
Yes you can, you jsut can't define it into xml layouts. You need to use it dynamically each time. Check this tutorial for instance.
In case link is dead, here is a sum up of the stuff :
Get a font file like times.otf
Drop it in your asset folder, inside a "fonts" folder
Get a reference of TextView with something like that:
TextView tv = (TextView) findViewById(R.id.myCustomTVFont);
Grab you font from the asset folder:
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/times.otf");
Make your TextView look great:
tv.setTypeface(tf);
As of Android 8.0 (API level 26), you can use fonts in XML. See the documentation here.
Your can take look in this thread as well to set custom fonts for all the views in your activity.