MainActivity.kt
package com.cfsuman.kotlintutorials
import android.graphics.Color
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the widgets from XML layout
val toolbar = findViewById<Toolbar>(R.id.toolbar);
textView = findViewById(R.id.textView);
// Configure the Toolbar
toolbar.apply {
title = "Toolbar title"
setTitleTextColor(Color.WHITE)
setLogo(R.drawable.ic_action_help)
//subtitle = "Toolbar Subtitle"
//setSubtitleTextColor(Color.WHITE)
}
// Set the toolbar as support action bar
setSupportActionBar(toolbar)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu to use in the action bar
val inflater = menuInflater
inflater.inflate(R.menu.toolbar_menu, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle presses on the action bar menu items
when (item.itemId) {
R.id.action_cut -> {
textView.text = "Cut Clicked."
return true
}
R.id.action_copy -> {
textView.text = "Copy Clicked."
return true
}
R.id.action_paste -> {
textView.text = "Paste Clicked."
return true
}
R.id.action_new -> {
textView.text = "New Clicked."
return true
}
}
return super.onOptionsItemSelected(item)
}
}
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:background="#F8F8F8">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:elevation="4dp"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="sans-serif"
android:textSize="24sp"
android:gravity="center" />
</androidx.constraintlayout.widget.ConstraintLayout>
Parent Theme
Theme.MaterialComponents.Light.NoActionBar
res/menu/toolbar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_cut"
android:title="Cut"
app:showAsAction="always"
android:icon="@drawable/ic_action_cut" />
<item
android:id="@+id/action_copy"
android:title="Copy"
app:showAsAction="always|withText"
android:icon="@drawable/ic_action_copy" />
<item
android:id="@+id/action_paste"
android:title="Paste"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_action_paste" />
<item
android:id="@+id/action_new"
android:title="New"
app:showAsAction="never"
android:icon="@drawable/ic_action_new" />
</menu>