Android how to set custom font
Android how to set custom font
I need to set a custom font (.ttf format) for whole app, how can i do it? From Manifest or XML would be the best option if it is possible
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
EDIT Sept 2014:
For anyone still seeing this old terrible answer, the real, good answer is here:
OLD:
Your best bet would be this:
You could do it like this. Create a custom textview and use that one everywhere
Add this to attrs.xml
Then in your theme, do this: This will make sure all the textviews will use the style MyTextView
And now we can define your custom font using your custom attribute in this style.
So whenever you use MyTextView in the project, it will have your custom font.
IMPORTANT: Typefaces are not cached, so if you plan on using this code, you should also build a custom typeface-cache so you can re-use the custom typeface for all textviews. This will significantly speed-up the application!
UPDATE: As Amir was saying, this is almost the same as Custom fonts and XML layouts (Android) but i also use android styling to automatically use it on all textviews in the app.
Fonts in XML
To use the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher, use the Support Library 26. For more information on using the support library, refer to the Using the support library section.
To add fonts as resources, perform the following steps in the Android Studio:
The New Resource Directory window appears.
Note: The name of the resource directory must be font.
Figure 1. Adding the font resource directory
Figure 2. Adding the font files in the resource directory
Figure 3. Previewing the font file
Creating a font family
A font family is a set of font files along with its style and weight details. In Android, you can create a new font family as an XML resource and access it as a single unit, instead of referencing each style and weight as separate resources. By doing this, the system can select the correct font based on the text style you are trying to use.
To create a font family, perform the following steps in the Android Studio:
Using fonts in XML layouts
Use your fonts, either a single font file or a font from a font family, in a TextView object or in styles. To add fonts to the TextView or in styles, use the fontFamily attribute.
Note: When you use a font family, the TextView switches on its own, as needed, to use the font files from that family.
Adding fonts to a TextView
Note: The Properties window is available only when the design editor is open. Select the Design tab at the bottom of the window.
Figure 4. Selecting the font from the Properties window
Figure 5. Previewing fonts in layout preview
Adding fonts to style
Using fonts programmatically
To retrieve fonts programmatically, call the getFont(int) method and provide the resource identifier of the font you want to retrieve. This method returns a Typeface object. Although the system picks the best style for you from the fonts information, you can use the setTypeface(android.graphics.Typeface, int) method to set the typeface with specific styles.
Note: The TextView already does this for you.
Kotlin
Using the support library
The Support Library 26.0 provides support to the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher.
Note: When you declare font families in XML layout through the support library, use the app namespace to ensure your fonts load.
To retrieve fonts programmatically, call the ResourceCompat.getFont(Context, int) method and provide an instance of Context and the resource identifier.
Kotlin
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
How to use custom font in a project written in Android Studio
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!
22 Answers 22
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Update 2021:
Create a folder named font inside the res folder and copy your font
All font names must be only: lowercase a-z, 0-9, or underscore.
For programmatic use:
For Android Studio 4.2+ there’s even now a menu option:
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.
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 the following things for that:
Android Studio above to 3.0 canary.
Your Activity extends AppCompatActivity.
Update your Gradle file like that:
buildtoolsVersion above to 26 and minimum targetSdkVersion required 26
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
How to set custom font for a whole application in Android?
Is it possible to set a custom font in an Android application?
I tried what is posted here, but I don’t know where my extends Application class is.
EDIT:
I tried the following:
Add a new class that extends from Application
I went to my style and added it.
MyApp.java:
AndroidManifest.xml:
styles.xml:
But my font is still not changning. any idea?
Then the MyApp class is called. But no effect on my fonts.
EDIT2: I realized that my buttons apply the custom font after I set a custom style for my buttons. Here is my custom button style:
And here is how it looks now:
15 Answers 15
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
1: Added «new resource directory» to the RES folder, Selected RESOURCE TYPE as «font» from the drop-down given, named the new directory «font» and saved.
2: Added my «custom_font.ttf» to the FONT folder just created.
3: Added my custom font in the application base theme in STYLES.XML
uk.co.chrisjenx:calligraphy Lib is not more maintained for latest android version alternative is now https://github.com/InflationX/Calligraphy
Add your custom fonts to assets/
For default font
Define your default font using CalligraphyConfig, in your Application class in the #onCreate() method and pass it to the CalligraphyInterceptor that you add to your ViewPump builder.
Inject into Context: Wrap the Activity Context:
Not more maintained by Developers Below Solution
There is a great library for custom fonts in android:Calligraphy
Here is a sample how to use it.
In Gradle you need to put this line into your app’s build.gradle file:
And then make a class that extends Application and write this code:
You should have made on assets/ a «New Directory» «fonts» (see below), so in that code «your font path» should be «fonts/SourceSansPro-Regular.ttf». (It’s just «fonts. » not «/fonts..» or «assets..»)
And in the activity class put this method before onCreate:
And the last thing your manifest file should look like this:
And it will change the whole activity to your font! it’s simple and clean!
Also dont forgot to add below two lines in attrs.xml,if you dont have attrs.xml file,create new file in values.xml
now next thing is in AndroidManifest.xml for the application tag give name for your application class. In this case it is MyApp
Update Put font file under assets/fonts/your_font_file.ttf
Put this line under onCreate method of your application class(MyApp)
Source File for TypefaceUtil
Now update your style.xml file
put the below line your style which is included for your activity in manifest file
Hope this helps
Android provides simpler and best solutions which is supported over API level 14 by Support Library 26+. Fonts in XML It also supports font family option. Required import : implementation «com.android.support:support-compat: «
Follow these simple steps and font family will be applied in your app without any hurdle:
Or use below to set typeface programmatically
You can do it with the android SDK now, as the Android Developer official youtube channel published in set/2017 here.
Requires API Level 23 and above.
You just need to put your font file in the res/font folder and reference them in your TextView in case you need a specific one with the android:fontFamily attribute.
If you want a base font on your whole app, simply put
in your styles.xml
You can download a custom font from Android Studio and on the go if you want as well. All of this you can find with details on the video above.
Go to project style file which exists in following path res/values/styles.xml and add styles like that:
Go to your code and write a class like this for changing whole app font:
Because fonts should separately set for each activities, I prefer write a class for it for more clean code.
Then call the class method before activity onCreate super.onCreate(savedInstanceState) :
Remember that if you want changing font during runtime, write a chosen font in SharedPreferences or etc, then pass chosen font to all activities setFont like above.
API 21 Android 5.0
I’ve investigated the reports in the comments that it doesn’t work and it appears to be incompatible with the theme android:Theme.Material.Light.
If that theme is not important to you, use an older theme, e.g.:
Add following in your app Theme:
Create class as TypefaceUtil
Call it in application class or in the Launcher activity
This is how I applied font ttf to entire App
1. Create a directory named font under res directory
2. Copy font ttf e.g. varela.ttf inside this font directory
3. Check the theme in AndroidManifest.xml
4. Go to themes.xml inside values directory and add the font with item tag inside base theme style referred in AndroidManifest.xml
First, make a new class that overrides whatever View you want to customize. (e.g. want a Button with a custom typeface? Extend Button ). For example:
Okay, so with that out of the way, let’s get back to the parseAttributes() method from earlier:
The xmlns:custom line can really be anything, but the convention is what’s shown above. What matters is that it is unique, and that’s why the package name is used. Now you just use the custom: prefix for your attributes, and the android: prefix for android attributes.
Here is my code:
Can anyone please get me out of this issue?
21 Answers 21
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
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
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
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.
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
Next we must create a font family:
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:
Источники информации:
- http://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
- http://stackoverflow.com/questions/27588965/how-to-use-custom-font-in-a-project-written-in-android-studio
- http://stackoverflow.com/questions/33923803/how-to-set-custom-font-for-a-whole-application-in-android
- http://stackoverflow.com/questions/3651086/android-using-custom-font