How to use custom fonts in your project
This demo will teach us how to use custom fonts in the android project. Follow the below steps to integrate custom fonts into your android project.
Step 1: Create New Project
Create New Project |
Create a folder named fonts under the assets folder and put all your fonts in that folder. (Folder name can be anything)
Step 3: Open the layout file and add TextView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rvContacts" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/txt_custom" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20sp" android:text="@string/hello_world" /> </RelativeLayout>
Step 4: Open Your Activity Class file and try the following code.
Here, we will get fonts from the project directory using the Typeface class
and then apply those fonts to TextView.
public class MainActivity extends Activity { TextView txt_custom; Typeface tface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); tface=Typeface.createFromAsset(getAssets(),"fonts/future.ttf"); txt_custom=(TextView)findViewById(R.id.txt_custom); txt_custom.setTypeface(tface); } }Now, Compile and run the project to see the result. You can see Textview fonts have been changed from regular fonts. See the below image for an example.