MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.widget.*
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout)
val textView = findViewById<TextView>(R.id.textView)
val bottomNavigationView = findViewById<BottomNavigationView>(
R.id.bottomNavigationView)
// Set navigation item selected listener
bottomNavigationView.setOnItemSelectedListener {
when(it.itemId){
R.id.attach_file -> {
textView.text = "Attach File Clicked."
true
}
R.id.delete -> {
textView.text = "Delete Clicked."
true
}R.id.insert_chart -> {
textView.text = "Insert Chart Clicked."
true
}R.id.insert_link -> {
textView.text = "Insert Link Clicked."
true
}R.id.backup ->{
textView.text = "Backup Clicked."
true
}else -> false
}
}
// Set navigation item re selected listener
bottomNavigationView.setOnItemReselectedListener {
when(it.itemId){
R.id.attach_file -> textView.text =
"Reselected Attach File."
R.id.delete -> textView.text =
"Reselected Delete."
R.id.insert_chart -> textView.text =
"Reselected Insert Chart."
R.id.insert_link -> textView.text =
"Reselected Insert Link."
R.id.backup ->textView.text =
"Reselected Backup."
}
}
// Set a click listener for root layout
rootLayout.setOnClickListener{
// Programmatically select bottom navigation bar menu item
bottomNavigationView.menu.getItem(3).isChecked = true
textView.text = "Programmatically Selected Insert Link."
}
// Set the bottom navigation view/bar background color
/*bottomNavigationView.setBackgroundColor(
Color.parseColor("#A2A2D0"))*/
}
}
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="#DCDCDC">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:gravity="center"
android:textSize="26sp"
android:padding="12dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/menu/bottom_navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/attach_file"
android:title="Attach File"
android:icon="@drawable/ic_action_attach_file"
/>
<item
android:id="@+id/delete"
android:title="Delete"
android:icon="@drawable/ic_action_delete"
/>
<item
android:id="@+id/insert_chart"
android:title="Insert Chart"
android:icon="@drawable/ic_action_insert_chart"
/>
<item
android:id="@+id/insert_link"
android:title="Insert Link"
android:icon="@drawable/ic_action_insert_link"
/>
<item
android:id="@+id/backup"
android:title="Backup"
android:icon="@drawable/ic_action_backup"
/>
</menu>



- kotlin - ImageView set image programmatically
- kotlin - Popup menu with icons example
- kotlin - Service example
- kotlin - AsyncTask with cancel progress example
- kotlin - WebView file download example
- kotlin - WebView image download example
- kotlin - AlertDialog setItems example
- kotlin - AlertDialog setSingleChoiceItems example
- kotlin - AlertDialog multiple choice example
- kotlin - AlertDialog yes no cancel button example