Android Kotlin: ActionBar background color programmatically

The code demonstrates how to programmatically set or change the background color of the ActionBar in an Android application. It leverages the Kotlin programming language and the Android framework.

Key components:

  • AppCompatActivity: The class represents the foundation of an Android activity that extends the functionality of the standard Activity class. It provides backward compatibility for features introduced in later versions of the Android platform.

  • supportActionBar: This property retrieves a reference to the ActionBar object, which is the bar at the top of the activity window that can contain the application's title, navigation button, and other action buttons.

  • setBackgroundDrawable(): This method sets the background drawable for the ActionBar. A drawable is a graphical resource that can be displayed on the screen. In this case, a ColorDrawable or GradientDrawable is used.

  • ColorDrawable: A drawable that represents a solid color. The ColorDrawable constructor takes a color integer as an argument.

  • GradientDrawable: A drawable that can display a gradient effect. The code defines a linear gradient with three colors.

Code walkthrough:

  1. Import statements: The code imports necessary classes from the Android framework, including Color, ColorDrawable, GradientDrawable, and Bundle.

  2. onCreate() method: The onCreate() method is the main entry point of the activity. It's called when the activity is first created.

    • The code sets the content view of the activity using setContentView(R.layout.activity_main). This method inflates the activity_main.xml layout file and sets it as the content view of the activity.

    • It retrieves a reference to the supportActionBar using supportActionBar?.apply. The safe null operator (?.) is used to avoid a NullPointerException if the supportActionBar is null.

    • The code sets the title of the ActionBar to "ActionBar background color" using the title property.

    • It creates a ColorDrawable object with a specific color value using Color.parseColor() and sets the background of the ActionBar using setBackgroundDrawable().

    • The code also includes a commented-out section that demonstrates how to set a gradient background for the ActionBar using the gradientDrawable() method. This method creates a GradientDrawable object with a linear gradient and a stroke, and then sets it as the background of the ActionBar.

  3. gradientDrawable() method: This private method is used to generate a GradientDrawable object with a linear gradient effect and a stroke.

In summary:

This code snippet provides a straightforward approach to customizing the ActionBar's background color programmatically in an Android Kotlin application. It offers the flexibility to use either a solid color or a gradient background.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
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 {
            title = "ActionBar background color"


            // Set action bar background color
            setBackgroundDrawable(
                ColorDrawable(
                    Color.parseColor("#8FBC8F")
                )
            )


            // We can also show gradient background in action bar
            // Uncomment below line to see gradient background
            //setBackgroundDrawable(gradientDrawable())
        }
    }
}



// Method to generate gradient drawable
private fun gradientDrawable(): GradientDrawable {
    return GradientDrawable().apply {
        colors = intArrayOf(
            Color.parseColor("#F0FFFF"),
            Color.parseColor("#00BFFF"),
            Color.parseColor("#89CFF0")
        )
        gradientType = GradientDrawable.LINEAR_GRADIENT
        shape = GradientDrawable.RECTANGLE
        setStroke(2,Color.parseColor("#2A52BE"))
    }
}
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