Android Kotlin: How to change ActionBar title text size

This is a demonstration of how to change the title text size of the ActionBar in an Android application written in Kotlin. The code utilizes the AppCompatActivity class and the supportActionBar property.

The code first checks if the support action bar is available using the supportActionBar?.apply block. If the action bar is available, the code creates a SpannableString instance containing the desired title text.

There are two methods for changing the title text size demonstrated in this code:

  • RelativeSizeSpan: This method allows you to scale the text size relative to its original size. In the code example, a RelativeSizeSpan is created with a value of 1.2F, which increases the text size by 20%.

  • AbsoluteSizeSpan: This method allows you to set the text size to a specific value in dips (density-independent pixels). In the code example, an AbsoluteSizeSpan is created with a value of 14dp.

The SpannableString object is then used to set the title of the action bar using the title property.

Finally, the code demonstrates how to display a logo on the action bar using the setDisplayShowHomeEnabled, setDisplayUseLogoEnabled, and setLogo methods.

Summary

This code snippet provides a straightforward approach to modifying the title text size and displaying a logo on the action bar in an Android Kotlin application. It showcases the use of SpannableString objects and different size span techniques to achieve the desired text size adjustments.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.text.style.RelativeSizeSpan
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        // If the support action bar is available
        supportActionBar?.apply {
            // Text for Action bar title
            val titleText = "  ActionBar Title Spannable"


            // Initialize a Spannable string instance
            val spannableString = SpannableString(titleText)

            // Scale title text size relative to original size
            // Increase title text size / font size
            val relativeSizeSpan = RelativeSizeSpan(1.2F)

            spannableString.setSpan(
                relativeSizeSpan,
                0,
                titleText.length,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE
            )

            // Show spannable title on action bar
            title = spannableString


            // Another way to change title text size
            val anotherTitle = "  Title By Absolute Size Span"
            val anotherSpannableSting = SpannableString(anotherTitle)

            // Absolute size span
            val absoluteSizeSpan = AbsoluteSizeSpan(
                14, // size in dip
                true // dip true
            )

            anotherSpannableSting.setSpan(
                absoluteSizeSpan,
                0,
                anotherSpannableSting.length,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE
            )

            // Comment and uncomment below line to test the effect
            //title = anotherSpannableSting


            // Show logo on action bar
            setDisplayShowHomeEnabled(true)
            setDisplayUseLogoEnabled(true)
            setLogo(R.drawable.ic_action_help)
        }
    }
}
activity_main.xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC" />
More android kotlin tutorials