Android Kotlin: How to change ActionBar title color

This code demonstrates how to set or change the color of the ActionBar title and display a logo on the ActionBar in an Android Activity.

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

  1. Import necessary libraries:

    • android.graphics.Color is imported to use color manipulation methods.
    • android.os.Bundle is imported to handle activity lifecycle events.
    • android.text.Spannable is imported to create a text with different styles like font color.
    • android.text.SpannableString is imported to create a CharSequence that can be modified with spans.
    • android.text.style.ForegroundColorSpan is imported to apply text color to a part of the text.
    • androidx.appcompat.app.AppCompatActivity is imported as the base class for the activity.
  2. Set up the Activity:

    • The code declares a class MainActivity that extends AppCompatActivity.
    • The onCreate method is overridden to handle the activity creation lifecycle event.
      • Inside onCreate, the layout for the activity is set using setContentView(R.layout.activity_main).
  3. Set the ActionBar title color:

    • The code checks if the support action bar is available using supportActionBar?.apply.
    • If the action bar is available, it creates a string titleText to set as the title.
    • A ForegroundColorSpan object is created with the desired color (Color.YELLOW in this case) to apply the color to the text.
    • A SpannableString object is created from the titleText.
    • The setSpan method is used to apply the ForegroundColorSpan to the entire titleText (from the 0th character to the length of the text, with inclusive indices).
    • Finally, the colored SpannableString is set as the title for the action bar using title = spannableString.
  4. Display logo on the ActionBar:

    • The methods setDisplayShowHomeEnabled(true), setDisplayUseLogoEnabled(true), and setLogo(R.drawable.ic_action_help) are used to display a logo on the action bar.
      • setDisplayShowHomeEnabled(true) enables the "home" button on the action bar.
      • setDisplayUseLogoEnabled(true) enables the use of a logo on the action bar.
      • setLogo(R.drawable.ic_action_help) sets the logo drawable resource (ic_action_help) to be displayed on the action bar.

In summary, this code effectively changes the ActionBar title color to yellow and displays a logo on the ActionBar in an Android Activity.

Note: The provided activity_main.xml file doesn't seem to be directly related to the functionality showcased in MainActivity.kt. It defines a basic ConstraintLayout that likely serves as the root layout for the activity.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
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 {
            // Set title text for action bar
            val titleText = "  ActionBar Colored Title"


            // Foreground color span to show colored title text
            val foregroundColorSpan = ForegroundColorSpan(
                Color.YELLOW)

            val spannableString = SpannableString(titleText)

            spannableString.setSpan(
                foregroundColorSpan,
                0,
                titleText.length,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE
            )

            // Show colored title on action bar
            title = spannableString


            // 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