I was trying to use custom font in Android Studio as we did in Eclipse. But unfortunately could not figure out where to put the 'assets' folder!
15 Answers
Here are the steps to solve it:
- Go to the (project folder)
- Then app>src>main
- Create folder 'assets>fonts' into the main folder.
Put your .ttf file into the fonts folder.
or try this way:
Select File>New>Folder>Assets Folder
Click finish
Right click on assets and create a folder called fonts
Put your font file in assets>fonts
Use code below to change your textView's font
There are many ways to set custom font family on field and I am using like that below.
To add fonts as resources, perform the following steps in the Android Studio:
1) Right-click the res folder and go to New > Android resource directory. The New Resource Directory window appears.
2) In the Resource type list, select font, and then click OK.
Fonts Ttf Untuk Android Studio
Note: The name of the resource directory must be font.
3) Add your font files in the font folder.
Add font in desired view in your xml file:
Note: But you required following things for that:
Android Studio above to 3.0 canary.
Your Activity extends AppCompatActivity.
Update you gradle file like that:
compileSdkVersion 26buildToolsVersion '26.0.1'defaultConfig {
minSdkVersion 19 targetSdkVersion 26 versionCode 1 versionName '1.0' testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'}- buildtoolsVersion above to 26 and minimum targetSdkVersion required 26
Add dependencies in build.gradle file:
classpath 'com.android.tools.build:gradle:3.0.0-beta4'
gradle-wrapper.properties:
distributionUrl=https://services.gradle.org/distributions/gradle-4.1-all.zip
I think instead of downloading .ttf file we can use Google fonts. It's very easy to implements. only you have to follow these steps.step 1) Open layout.xml of your project and the select font family of text view in attributes (for reference screen shot is attached)
step 2) The in font family select More fonts. option if your font is not there. then you will see a new window will open, there you can type your required font & select the desired font from that list i.e) Regular, Bold, Italic etc. as shown in below image.
step 3) Then you will observe a font folder will be auto generated in /res folder having your selected fonts xml file.
Then you can directly use this font family in xml as
or pro grammatically you can achieve this by using
Hello here we have a better way to apply fonts on EditTexts and TextViews on android at once and apply it in whole project.
First of All you need to make fonts folder. Here are Steps.
1: Go to the (project folder) Then app>src>main
2: Create folders named 'assets/fonts' into the main folder.
3: Put your fonts into the fonts folder. Here I Have 'MavenPro-Regular.ttf'
Here are the Steps for applying custom fonts on EditText and using this approach you can apply fonts on every input.
1 : Create a Class MyEditText (your preferred name ..)
2 : which extends EditText
3 : Apply your font
Here is code Example;
And in Here is the code how to use it.
Or in Your xml File
With Support Library 26.0 (and Android O,) fonts can be loaded from resource easily with
More info can be found here.
You can use easy & simple EasyFonts third party library to set variety of custom font to your TextView
. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation. You will be free from creating asset folder too.
Simply:
There are many type of fonts provided by this library.
I want to add my answer for Android-O and Android Studio 2.4
Create folder called font under res folder. Download the various fonts you wanted to add to your project example Google fonts
Inside your xml user font family
example :
3.If you want it to be in programmatic way use following code
for more information follow the link to my blog post Font styles for Android with Android Studio 2.4
As per new feature available in Android O, font resources in XML is avilable as new feature.
To add fonts as resources, perform the following steps in the Android Studio:
1) Right-click the res folder and go to New > Android resource directory. The New Resource Directory window appears.
2) In the Resource type list, select font, and then click OK.
Note: The name of the resource directory must be font.
3) Add your font files in the font folder.
You can access the font resources with the help of a new resource type, font. For example, to access a font resource, use @font/myfont, or R.font.myfont.
eg. Typeface typeface = getResources().getFont(R.font.myfont);textView.setTypeface(typeface);
- Create folder assets in Project -> app (or your app name) -> src -> main -> right click -> New -> Directory.
- Then create a new directory inside assets called 'fonts'.
To assign the font to the textView:
your_font_name includes font extension.
If you are very new to Android like I am this can be a little tricky. Make sure you call:
method within a method such as onCreate
.
Android 8.0 (API 26) introduced new features related to fonts.
1) Fonts can be used as resources.
2) Downloadable fonts.
If you want to use external fonts in your android application, you can either include font files in apk or configure downloadable fonts.
Including font files in APK : You can download font files, save them in res/font filer, define font family and use font family in styles.
For more details on using custom fonts as resources see http://www.zoftino.com/android-using-custom-fonts
Configuring downloadable fonts : Define font by providing font provider details, add font provider certificate and use font in styles.
For more details on downloadable fonts see http://www.zoftino.com/downloading-fonts-android
First create assets
folder then create fonts
folder in it.
Then you can set font
from assets
or directory
like bellow :
For new readers
You can use this library Gloxey Custom Font Views
gradle dependency
How to use?
Create folder assets ->fonts. Copy your fonts into fonts folder.
Use property app : font_name = 'font_name_string' to apply font on view.
Example
1st add font.ttf file on font Folder. Then Add this line in onCreate method
And here is my xml
protected by Nilesh RathodMay 14 '18 at 4:48
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged androidandroid-studio or ask your own question.
Custom fonts in Android an OK way.
Are you fed up of Custom Views to set fonts? Or traversing the ViewTree to find TextViews? Yeah me too.
Getting started
Dependency
Include the dependency Download (.aar) :
Add Fonts
Add your custom fonts to assets/
. All font definitions are relative to this path.
Assuming that you are using Gradle you should create the assets directory under src/main/
in your project directory if it does not already exist.As it's popular to use multi-project build with Gradle the path is usually app/src/main/assets/
, where app
is the project name.
You might consider creating a fonts/
subdirectory in the assets directory (as in examples).
Usage
Note: The missing namespace, this IS intentional.
Installation
Define your default font using CalligraphyConfig
, in your Application
class in the #onCreate()
method.
Note: You don't need to define CalligraphyConfig
but the library will applyno default font and use the default attribute of R.attr.fontPath
.
Inject into Context
Wrap the Activity
Context:
You're good to go!
Usage
Custom font per TextView
Note: Popular IDE's (Android Studio, IntelliJ) will likely mark this as an error despite being correct. You may want to add tools:ignore='MissingPrefix'
to either the View itself or its parent ViewGroup to avoid this. You'll need to add the tools namespace to have access to this 'ignore' attribute. xmlns:tools=' http://schemas.android.com/tools'
. See https://code.google.com/p/android/issues/detail?id=65176.
Custom font in TextAppearance
Custom font in Styles
Custom font defined in Theme
Font Resolution
The CalligraphyFactory
looks for the font in a pretty specific order, for the most part it'svery similar to how the Android framework resolves attributes.
View
xml - attr defined here will always take priority.Style
xml - attr defined here is checked next.TextAppearance
xml - attr is checked next, the only caveat to this is IF you have a fontdefined in theStyle
and aTextAttribute
defined in theView
theStyle
attribute is picked first!Theme
- if defined this is used.Default
- if defined in theCalligraphyConfig
this is used of none of the above are foundOR if one of the above returns an invalid font.
Why not piggyback off of fontFamily attribute?
We originally did, but it conflicted with users wanting to actually use that attribute, you nowhave to define a custom attribute.
Why no jar?
We needed to ship a custom ID with Calligraphy to improve the Font Injection flow. Thisunfortunately means that it has to be an aar
. But you're using Gradle now anyway right?
Multiple Typeface's per TextView / Spannables
It is possible to use multiple Typefaces inside a TextView
, this isn't new concept to Android.
This could be achieved using something like the following code.
Of course this is just an example. Your mileage may vary.
Exceptions / Pitfalls
To our knowledge (try: grep -r -e 'void set[^(]*(Typeface ' <android source dir>
) there are two standard Android widgets that have multiple methods to set typefaces. They are:
- android.support.v7.widget.SwitchCompat
- android.widget.Switch
Both have a method called setSwitchTypeface
that sets the typeface within the switch (e.g. on/off, yes/no). SetTypeface
sets the typeface of the label. You will need to create your own subclass that overrides setTypeface
and calls both super.setTypeface
and super.setSwitchTypeface
.
This library was created because it is currently not possible to declare a custom font in XML files in Android.
If you feel this should be possible to do, please star this issue on the official Android bug tracker.
I applied a custom font to a TextView
, but it doesn't seems to change the typeface.
Here is my code:
Can anyone please get me out of this issue?
21 Answers
On Mobiletuts+ there is very good tutorial on Text formatting for Android. Quick Tip: Customize Android Fonts
EDIT: Tested it myself now. Here is the solution. You can use a subfolder called fonts but it must go in the assets
folder not the res
folder. So
assets/fonts
Also make sure that the font ending I mean the ending of the font file itself is all lower case. In other words it should not be myFont.TTF
but myfont.ttf
this way must be in lower case
After trying most of the solutions described in this thread, I accidentally found Calligraphy (https://github.com/chrisjenx/Calligraphy) - a library by Christopher Jenkins that lets you easily add custom fonts to your app. The advantages of his lib comparing to approaches suggested here are:
- you don't have to introduce your own overriden TextView component, you use the built-in TextView
- you can easily include the library using gradle
- The library doesn't limit your choice of fonts; you just add your preferred ones to the assets dir
- you not only get custom text views — all the other text-based Android compontents will also be displayed using your custom font.
I know there are good answers already, but here's a fully working implementation.
Here's the custom text view:
Here's the custom attributes. This should go to your res/attrs.xml
file:
And here's how you use it. I'll use a relative layout to wrap it and show the customAttr
declaration, but it could obviously be whatever layout you already have.
I've successfully used this before. The only difference between our implementations is that I wasn't using a subfolder in assets. Not sure if that will change anything, though.
Provided that you placed the font in the right place and there is no error in the font file itself, your code should work like that, RATTLESNAKE.
However, it would be a lot easier if you could just define a font in your layout xml, like this:
With the accompanying res/values/styles.xml
:
I created a couple of tools specifically for this purpose. Refer to this project from GitHub, or take a look at this blog post which explains the whole thing.
For Custom Fonts in android create a folder within assets folder name it 'fonts' place your desired fonts.ttf or .otf file in it.
If you extends UIBaseFragment:
else if extends Activity:
The best way to do it From Android O preview release is this way:
It works only if you have android studio-2.4 or above
- Right-click the res folder and go to New > Android resource directory. The New
Resource Directory window appears. - In the Resource type list, select font, and then click OK.
- Add your font files in the font folder.The folder structure below generates
R.font.dancing_script
,R.font.la_la
, andR.font.ba_ba
. - Double-click a font file to preview the file's fonts in the editor.
Next we must create a font family:
- Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
- Enter the File Name, and then click OK. The new font resource XML opens in the editor.
Enclose each font file, style, and weight attribute in the font tag element. The following XML illustrates adding font-related attributes in the font resource XML:
Adding fonts to a TextView:
As from the documentation
All the steps are correct.
You can use PixlUI at https://github.com/neopixl/PixlUI
import their .jar and use it in XML
Since I was not satisfied with all the presented solutions on SO, I've come up with mine. It's based on a little trick with tags (i.e. you can't use tags in your code), I put the font path there. So when defining views, you can do either this:
or this:
Now you can either explicitly access / setup the view as:
or just setup everything via:
And what is the magic class you ask? Mostly glued from another SO posts, with helper methods for both activity and fragments:
Unfortunately there is no good solution for this.
I've seen the many articles about using a custom TextView but what they forget it that it's not only textviews that can implement fonts & there are textviews hidden away in other views inaccessible to the developer; I'm not even going to get started on Spannable.
You could use an external font utility like:
BUT This loops over every view in the application on it's creation and even this utility misses some views (ViewPager renders normal font) then you have the problem that is when Google updates their build tools this will occasionally crash because it needs to target deprecated properties. It's also a little slow as it uses Java's Reflection.
It's really up to Google to fix this. We need better font support in Android. If you look at the solution from iOS they literally have 100's of fonts built in to select from. Want a custom font? Simply drop a TFF in and it's usable.
For now were now limited to the offering that Google offers us which is extremely limited but fortunately mobile optimized.
Make sure to paste the above code into onCreate() after your call to the super and the call to setContentView(). This small detail kept my hung up for awhile.
With Android 8.0 using Custom Fonts in Application became easy with downloadable fonts
.We can add fonts directly to the res/font/ folder
in the project folder, and in doing so, the fonts become automatically available in Android Studio.
Now set fontFamily
attribute to list of fonts or click on more and select font of your choice. This will add tools:fontFamily='@font/your_font_file'
line to your TextView.
This will Automatically generate few files.
1. In values folder it will create fonts_certs.xml
.
2. In Manifest it will add this lines:
3.preloaded_fonts.xml
I had the same problem, the TTF did not show up. I changed the font file, and with the same code it's working.
If you want to load the font from the network or easily style it, you can use:
Example:
Well, after seven years you can change whole app textView
or what you want easily by using android.support
libraries 26++.
E.g:
Create your font package app/src/res/font and move your font into it.
And in your app theme just add it as a fontFamily:
Example for use with textView
only:
And add into your main theme:
Currently it's worked on 8.1 until 4.1 API Jelly Bean And that's a wide range.
Update answer:Android 8.0 (API level 26) introduces a new feature, Fonts in XML.just use the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher, use the Support Library 26.
see this link
Old answer
There are two ways to customize fonts :
!!! my custom font in assets/fonts/iran_sans.ttf
Way 1 :Refrection Typeface.class ||| best way
call FontsOverride.setDefaultFont() in class extends Application, This code will cause all software fonts to be changed, even Toasts fonts
AppController.java
FontsOverride.java
Way 2: use setTypeface
for special view just call setTypeface() to change font.
CTextView.java
FontUtils.java
You can use easy & simple EasyFonts third party library to set variety of custom fonts to your TextView
. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation.
Instead of
Simply:
This library also provides following font face.
- Roboto
- Droid Serif
- Droid Robot
- Freedom
- Fun Raiser
- Android Nation
- Green Avocado
- Recognition
The correct way of doing this as of API 26 is described in the official documentation here :
This involves placing the ttf files in res/font folder and creating a font-family file.
The most simple solution android supported now!
Use custom font in xml:
look details:
- Open your project and select Project on the top left
- app -->src -->main
- right click to main and create directory name it as assets
- right click to assest and create new directory name it fonts
- you need to find free fonts like free fonts
- give it to your Textview and call it in your Activity class
- copy your fonts inside the fonts folder
TextView txt = (TextView) findViewById(R.id.txt_act_spalsh_welcome); Typeface font = Typeface.createFromAsset(getAssets(), 'fonts/Aramis Italic.ttf'); txt.setTypeface(font);
name of the font must be correct and have fun
Yes, downloadable fonts are so easy, as Dipali s said.
This is how you do it..
- Place a
TextView
. - In the properties pane, select the
fontFamily
dropdown. If it isn't there, find the caret thingy (the > and click on it to expandtextAppearance
) under the. - Expand the
font-family
drop down. - In the little list, scroll all the way down till you see
more fonts
- This will open up a dialog box where you can search from
Google Fonts
- Search for the font you like with the search bar at the top
- Select your font.
- Select the style of the font you like (i.e. bold, normal, italic, etc)
- In the right pane, choose the radio button that says
Add font to project
- Click okay. Now your TextView has the font you like!
BONUS:If you would like to style EVERYTHING with text in your application with chosen font, just add <item name='android:fontfamily'>@font/fontnamehere</item>
into your styles.xml
protected by Community♦Feb 17 '16 at 16:51
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged androidcustom-fontandroid-fontsandroid-typeface or ask your own question.
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.
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.
2 Answers
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:
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
The TextView
See this link for more information.
Not the answer you're looking for? Browse other questions tagged androidfontsitext or ask your own question.
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.
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.
2 Answers
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:
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
The TextView
See this link for more information.