Open In App

How to Change Button Font in Android?

Last Updated : 30 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A Button in Android is a UI element provided to a user to click to perform a certain action. A text can be set inside the button to name it. However, this text can be seen in a particular font only which is set by default.

 Change Button Font in Android

So in this article, we will show you how you could change the Button text font in Android. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a Button in this file.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="CLick to change font"/>
  
</RelativeLayout>


Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package org.geeksforgeeks.changebuttonfont
  
import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring and initializing
        // Button from the layout file
        val mButton = findViewById<Button>(R.id.button_1)
  
        // Declaring integer variable
        // to use in when loop
        var clicks = 0
  
        // When button is clicked
        mButton.setOnClickListener {
            // Variable is incremented
            clicks += 1
            // Enters when loop and changes typeface
            when(clicks){
                1-> {mButton.setTypeface(Typeface.MONOSPACE)
                    mButton.text = "Monospace"}
  
                2-> {mButton.setTypeface(Typeface.SANS_SERIF)
                    mButton.text = "Sans Serif"}
  
                3-> {mButton.setTypeface(Typeface.DEFAULT_BOLD)
                    mButton.text = "Default Bold"}
  
                4-> {mButton.setTypeface(Typeface.SERIF)
                    mButton.text = "Serif"}
  
                5-> {mButton.setTypeface(Typeface.DEFAULT)
                    mButton.text = "Default"
                    // Returns back to the top (Monospace)
                    clicks = 0}
            }
        }
    }
}


Output:

You can see that on the button click, the button font changes.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads