Android Kotlin: How to change ActionBar title style

This code demonstrates how to set or change the style of the ActionBar title in an Android application. The code achieves this by creating a custom TextView and setting its properties, such as text, layout parameters, gravity, appearance, text color, font family, and text style.

Here's a step-by-step breakdown of the code:

  1. Import necessary libraries: The code starts by importing necessary libraries like android.graphics.Color, android.graphics.Paint, android.graphics.Typeface, etc. These libraries are used to manipulate the visual aspects of the ActionBar title, such as color, font, and style.

  2. Create a custom TextView: The actionBarCustomView method creates a TextView instance and sets its properties. The text property is set to "ActionBar Title Style". The layout parameters of the TextView are set to match_parent width and wrap_content height. The gravity is set to center to center-align the title text within the ActionBar.

  3. Set text appearance, color, and font style: The code sets the text appearance of the TextView using setTextAppearance. This method applies the default Material Design style for the ActionBar title. The text color is set to white using setTextColor. The font family is set to sans-serif using typeface. Additionally, the code underlines the title text using paintFlags and sets the typeface to bold italic using setTypeface.

  4. Apply the custom view to the ActionBar: In the onCreate method, the code checks if the support action bar is available. If it is, the following steps are performed:

    • Set the custom view created in actionBarCustomView method to the ActionBar using customView.
    • Set the display options of the ActionBar to show custom content using displayOptions.
    • Enable the display of the home button and the app logo using setDisplayShowHomeEnabled and setDisplayUseLogoEnabled methods respectively.
    • Set the app logo using setLogo.

In summary, this code snippet provides a way to customize the look and feel of the ActionBar title in an Android application by creating a custom TextView and applying various styling options to it. This can be useful for creating a unique visual identity for your app and enhancing the user experience.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.os.Bundle
import android.view.Gravity
import android.widget.TextView
import androidx.appcompat.app.ActionBar.DISPLAY_SHOW_CUSTOM
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams


class MainActivity : AppCompatActivity() {
    lateinit var context:MainActivity

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

        // Get the context
        context = this


        // If the support action bar is available
        supportActionBar?.apply {
            // Show custom title in action bar
            customView = actionBarCustomView()

            // Action bar more settings
            displayOptions = DISPLAY_SHOW_CUSTOM
            setDisplayShowHomeEnabled(true)
            setDisplayUseLogoEnabled(true)
            setLogo(R.drawable.ic_action_help)
        }
    }



    // Method generate custom view for action bar title
    private fun actionBarCustomView():TextView{
        return TextView(context).apply {
            // Action bar title text
            text = "ActionBar Title Style"


            // Initialize layout parameters for text view
            val params = LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT
            )

            // Apply layout params for text view
            layoutParams = params


            // Center align the text
            gravity = Gravity.CENTER


            // Title text appearance
            setTextAppearance(
                android.R.style
                    .TextAppearance_Material_Widget_ActionBar_Title
            )


            // Action bar title text color
            setTextColor(Color.WHITE)

            // Title font family
            typeface = Typeface.SANS_SERIF


            // Underline the title text
             paintFlags = Paint.UNDERLINE_TEXT_FLAG

            // Bold title text
            // setTypeface(typeface,Typeface.BOLD)

            // Italic title text
            // setTypeface(typeface,Typeface.ITALIC)


            // Bold italic title text
            setTypeface(typeface,Typeface.BOLD_ITALIC)
        }
    }
}
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