How to Change the Typeface of TextView in Android?

A typeface is a design for alphabets that differs from other typefaces in terms of style, size, and weight. In general, your device or software contains a large number of local typefaces for use. However, there are many more typefaces available on the Internet that may be downloaded and used for many purposes of mobile application.


Step 1: Create a new project in Android  or you can use an already created project.  


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

 implement the same invoke the following code inside activity_main.xml file. 

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imgPiechart"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="180dp"
android:src="@drawable/company_logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tvfontface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imgPiechart"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:text="Candidroot solutions"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgPiechart" />

</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Create an MainActivity.java file in your package folder .

 Implement the same invoke the following code inside MainActivity.java file.

 import android.annotation.SuppressLint
import android.graphics.Typeface
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivityTwo : AppCompatActivity() {

@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_two)
val mTextView = findViewById<TextView>(R.id.tvfontface)
// Creating a typeface
val font = Typeface.createFromAsset(assets, "jellybomb.ttf")
// Setting the TextView typeface
mTextView.typeface = font
}
}
Step 4: Output of above example .


Happy coding!

365Bloggy May 31, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive