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:
Import necessary libraries:
android.graphics.Coloris imported to use color manipulation methods.android.os.Bundleis imported to handle activity lifecycle events.android.text.Spannableis imported to create a text with different styles like font color.android.text.SpannableStringis imported to create a CharSequence that can be modified with spans.android.text.style.ForegroundColorSpanis imported to apply text color to a part of the text.androidx.appcompat.app.AppCompatActivityis imported as the base class for the activity.
Set up the Activity:
- The code declares a class
MainActivitythat extendsAppCompatActivity. - The
onCreatemethod is overridden to handle the activity creation lifecycle event.- Inside
onCreate, the layout for the activity is set usingsetContentView(R.layout.activity_main).
- Inside
- The code declares a class
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
titleTextto set as the title. - A
ForegroundColorSpanobject is created with the desired color (Color.YELLOWin this case) to apply the color to the text. - A
SpannableStringobject is created from thetitleText. - The
setSpanmethod is used to apply theForegroundColorSpanto the entiretitleText(from the 0th character to the length of the text, with inclusive indices). - Finally, the colored
SpannableStringis set as the title for the action bar usingtitle = spannableString.
- The code checks if the support action bar is available using
Display logo on the ActionBar:
- The methods
setDisplayShowHomeEnabled(true),setDisplayUseLogoEnabled(true), andsetLogo(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.
- The methods
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" />