How to use custom font in a project written in Android Studio
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.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/abc_font" />
For programmatic use:
textView.setTypeface(ResourcesCompat.getFont(context, R.font.abc_font))
For Android Studio 4.2+ there's even now a menu option:
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:
compileSdkVersion 26
buildToolsVersion "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
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
TextView textView = (TextView) findViewById(R.id.textView); Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/yourfont.ttf"); textView.setTypeface(typeface);