Posts

Customize the ActionBar in Your Android App: A Guide to Changing Background Color, Title Text Color, and Text Size Programmatically Using Kotlin

The ActionBar is an important part of the Android user interface that provides users with easy access to key features and functions of an app. To make your app stand out, you may want to customize the appearance of the ActionBar. In this blog post, we'll explore how to change the background color, title text color, and title text size of the ActionBar in an Android Studio Kotlin project.



To change the background color, title text color, and title text size of the ActionBar in an Android Studio, just add the following code in your MainActivity.kt file.


Imports


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.graphics.drawable.ColorDrawable
import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import androidx.core.content.ContextCompat





MainActivity.kt


supportActionBar?.let { actionBar ->

actionBar.setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(this, R.color.red)))
val title = SpannableString("My ActionBar Title")
title.setSpan(ForegroundColorSpan(ContextCompat.getColor(this, com.google.android.material.R.color.m3_ref_palette_white)), 0, title.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
title.setSpan(AbsoluteSizeSpan(24, true), 0, title.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
supportActionBar?.title = title

}





And done. Now run your app.








Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.