MainActivity.kt
package com.cfsuman.kotlintutorials
import android.graphics.Color
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
// Initialize a counter variable
var counter = 0;
// If the support action bar is available
supportActionBar?.apply {
// Show custom title in action bar
customView = actionBarCustomTitle()
// Action bar title click listener
customView.setOnClickListener {
counter++
(it as TextView).text =
"Title clicked $counter time(s)"
}
// More action bar settings
displayOptions = DISPLAY_SHOW_CUSTOM
setDisplayShowHomeEnabled(true)
setDisplayUseLogoEnabled(true)
setLogo(R.drawable.ic_action_help)
}
}
// Method to create a custom title for action bar
private fun actionBarCustomTitle():TextView{
return TextView(context).apply {
// Set action bar title text
text = "ActionBar Click Listener"
// Initialize the layout params instance
val params = LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT
)
// Apply the layout parameters to the text view
layoutParams = params
// Center align the text view/ action bar title
gravity = Gravity.CENTER
// Set the action bar title text appearance
setTextAppearance(
android.R.style
.TextAppearance_Material_Widget_ActionBar_Title
)
// Set the action bar title text color
setTextColor(Color.WHITE)
}
}
}
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" />


- android kotlin - Circular bitmap with border
- android kotlin - Bitmap crop circular area
- android kotlin - Bitmap add border
- android kotlin - ThumbnailUtils example
- android kotlin - Base64 string to bitmap
- android kotlin - ImageView grayscale filter
- android kotlin - Bitmap invert colors
- android kotlin - Drawable tint programmatically
- android kotlin - Set image from drawable
- android kotlin - ActionBar title text size
- android kotlin - ActionBar title color
- android kotlin - ActionBar title center
- android kotlin - Get ActionBar height
- android kotlin - Checkable menu item
- android kotlin - Popup menu with icons