Sunday, March 11, 2018

android kotlin - Bottom navigation bar example

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.graphics.Color
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        /*
            *** reference source developer.android.com ***

            BottomNavigationView
                Represents a standard bottom navigation bar for application.
                Bottom navigation bars make it easy for users to explore and switch between
                top-level views in a single tap. It should be used when application has
                three to five top-level destinations.

                The bar contents can be populated by specifying a menu resource file. Each menu
                item title, icon and enabled state will be used for displaying bottom navigation
                bar items. Menu items can also be used for programmatically selecting which
                destination is currently active. It can be done using MenuItem#setChecked(true)
        */

        // Set a navigation item selected listener for bottom navigation view
        bottom_navigation_view.setOnNavigationItemSelectedListener {
            when(it.itemId){
                R.id.attach_file -> {
                    text_view.text = "Attach File Clicked."
                    true
                }
                R.id.delete -> {
                    text_view.text = "Delete Clicked."
                    true
                }R.id.insert_chart -> {
                text_view.text = "Insert Chart Clicked."
                true
                }R.id.insert_link -> {
                    text_view.text = "Insert Link Clicked."
                    true
                }R.id.backup ->{
                    text_view.text = "Backup Clicked."
                    true
                }else -> false
            }
        }


        // Set a navigation item re selected listener for bottom navigation view
        bottom_navigation_view.setOnNavigationItemReselectedListener {
            when(it.itemId){
                R.id.attach_file -> text_view.text = "Reselected Attach File."
                R.id.delete -> text_view.text = "Reselected Delete."
                R.id.insert_chart -> text_view.text = "Reselected Insert Chart."
                R.id.insert_link -> text_view.text = "Reselected Insert Link."
                R.id.backup ->text_view.text = "Reselected Backup."
            }
        }


        // Set a click listener for root layout
        root_layout.setOnClickListener{
            // Programmatically select bottom navigation bar menu item
            bottom_navigation_view.menu.getItem(3).isChecked = true
            text_view.text = "Programmatically Selected  Insert Link."
        }


        // Set the bottom navigation view/bar background color
        bottom_navigation_view.setBackgroundColor(Color.YELLOW)
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cddacd"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:textSize="30sp"
        android:gravity="center"
        />
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:menu="@menu/bottom_navigation_menu"
        app:itemIconTint="#3dc72a"
        app:itemTextColor="#dc6714"
        />
</LinearLayout>
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>
More android examples