How to use Roboto font in android Project
You can download the Roboto font from here:
https://fonts.google.com/specimen/Roboto. [Updated 2020-01-28]
You can do it the conventional way by using TypeFace
, like this:
Typeface typeface = Typeface.createFromAsset(getAssets(), fontName);
textView.setTypeface(typeface);
Note: The above will have to be done in every Activity
.
Alternatively, if, for example, you want to apply the Roboto font to all the TextView
s in your application, then you will need to create your own widget that extends TextView
.
There is a simple way of doing this. Follow the steps from this answer on SO: https://stackoverflow.com/a/9199258/450534 (full props to leocadiotine for the solution. I have used it before and it works like a charm)
EDIT: Think of your_namespace
as a marker for you to give it a name of your choice. For example, when integrating Admob in XML, I use xmlns:ads
. You can use, for example: xmlns:font
or something descriptive.
As for what the custom.ttf stands for, it is basically the font file with its extension that you need to copy in your Assets
folder. For example, if you are using ROBOTO-REGULAR.TTF, then replace the custom.ttf with ROBOTO-REGULAR.TTF. Using this example, the entire code should look this this:
<your.package.widget.TypefacedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res/your.package"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Custom fonts in XML are easy"
android:textColor="#FFF"
android:textSize="14dip"
font:typeface="ROBOTO-REGULAR.TTF" />
How to use Roboto in xml layout
I've already found some possibilities
Using fontfamily
The simplest way would be add fontFamily
attribute to your specific view
like TextView
According to How to change fontFamily of TextView in Android
From android 4.1 / 4.2 / 5.0, the following
Roboto font families are available:android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)
http://developer.android.com/reference/android/widget/TextView.html#attr_android:typeface
in combination with
android:textStyle="normal|bold|italic"
this 14 variants are possible:
- Roboto regular
- Roboto italic
- Roboto bold
- Roboto bold italic
- Roboto-Light
- Roboto-Light italic
- Roboto-Thin
- Roboto-Thin italic
- Roboto-Condensed
- Roboto-Condensed italic
- Roboto-Condensed bold
- Roboto-Condensed bold italic
- Roboto-Medium
- Roboto-Medium italic
You can also do this programmatically using code as below:
textView.setTypeface(Typeface.create("sans-serif-thin", Typeface.NORMAL));
Using typeface
Available built-ibn fonts are:
- normal
- sans
- serif
- monospace
You can cobine them like below:
android:typeface="sans" | "serif" | "monospace"
See android:typeface.
Using styles.xml
You set style in styles.xml` like that:
<style name="boldText">
<item name="android:textStyle">bold|italic</item>
<item name="android:textColor">#FFFFFF</item>
</style>
and to use this style in main.xml
layout file just use:
style="@style/boldText"
Combining Text atrributes
You can mix TextView
attributes like in code below:
android:fontFamily="serif"
android:textStyle="italic"
Using third-party libraries
Foundry
- apply custom typefaces through XML layouts and styles.
android-typeface-helper
- Typeface helper for Android
Additional lecture
You may also want to read about Roboto
typeface and Typography Google's Design Guide.
- Using Roboto thin or condensed
- How do I specify eg. Roboto-Medium or Roboto-Black in styles.xml
Use Roboto font in app with minimum API level 14
Hope it help
How to set Roboto variant font family to a Textview in android?
For any font family,We have to download that file(.ttf format) and add that file into our project directory.That too under the font directory.After adding those files,we can use that in our textview using android:fontFamily
attribute.
Setting Roboto to my global font for my app in Android Studio
The answer is yes.
Global Roboto light for TextView and Buttonclasses.
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
<item name="android:buttonStyle">@style/RobotoButtonStyle</item>
</style>
<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">sans-serif-light</item>
</style>
<style name="RobotoButtonStyle" parent="android:Widget.Holo.Button">
<item name="android:fontFamily">sans-serif-light</item>
</style>
Just select the style you want from list themes.xml then create your custom style based on the original. At the end apply the style as the theme of the application.
<application android:theme="@style/AppTheme" > </application>
It will works only with built-in fonts like Roboto, but that was the question. For custom fonts (loaded from assets for example) this method will not work.
And you can use this text view class in your XML file. Have a look at this thread
https://stackoverflow.com/a/16406494/1841777
How can I use Roboto font via XML and styles tag only in android
STEP 1/
Start by creating a folder named assests
then inside that folder create another one named folder
and import your *.ttf
files to that folder
STEP 2/
Now import this before start writing the code given below:
import android.graphics.Typeface;
Now implement the following code to your class:
// Font path
String fontPath = "fonts/Face Your Fears.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.ghost);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
I suggest you follow this tutorial right here it will guide step-by-step through using external fonts in Android Studio
Setting Roboto font in TextView - xml
Take a look at the RobotoTextView project. Works down to Android 1.5, and you can set the typeface using XML attributes. It also includes other views like RobotoButton, RobotoCheckbox, etc.
Related Topics
View Binding - How to Get a Binding for Included Layouts
Android Id Naming Convention: Lower Case with Underscore VS. Camel Case
Android Recyclerview: Change Layout File List to Grid Onoptionitemselected
Android: Looking for a Drawarc() Method with Inner & Outer Radius
How to Bring View in Front of Everything
How to Display Multiple Notifications in Android
Otp (Token) Should Be Automatically Read from the Message
Don't Collapse Toolbar When Recyclerview Fits the Screen
Want to Compile Native Android Binary I Can Run in Terminal on the Phone
How to Use Roboto Font in Android Project
Okhttp Response Callbacks on the Main Thread
Change Expandable Indicator in Expandablelistview
Support Library Vectordrawable Resources$Notfoundexception
How to Work with Android's In-App Update API