Ad

How To Add A Click Listener To Menu Item In Kotlin

I want to create a Bottom Navigation Bar. I used android BottomNavigationView to create the UI but I don't know how to add an OnClick listener to the items in Menu.

I searched on google and found some articles, but all of them were using a Toolbar element. I don't know about how to add that and what it was doing.

This is my Navigation bar code which I am including in the main activity

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:design="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
             android:layout_height="match_parent">


    <RelativeLayout android:layout_width="match_parent" android:id="@+id/bottom_nav"
                    android:background="@drawable/gradient_theme"
                    android:layout_height="wrap_content"  android:layout_gravity="bottom">

        <android.support.design.widget.BottomNavigationView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/white"
                android:id="@+id/menuBar"
                design:menu="@menu/menu_bar"
                design:itemIconTint="@android:color/darker_gray"/>
    </RelativeLayout>
</FrameLayout>

This is my MainActivity.kt in which i want to set Listeners

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        beautifyLayout(this, window)
        setSupportActionBar(toolbar)

        testButton.setOnClickListener {
            val intent= Intent(this,AccountActivity::class.java)
            finish()
            startActivity(intent)
        }
    }
    }

This is menu_bar.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/btn1" android:title="" android:icon="@drawable/ic_feed"/>
    <item android:id="@+id/btn2" android:title="" android:icon="@drawable/ic_chat_bubble_black_24dp"/>
    <item android:id="@+id/btn3" android:title="" android:icon="@drawable/ic_search_black_24dp"/>
    <item android:id="@+id/btn4" android:title="" android:icon="@drawable/ic_menu_black_24dp" />
</menu>

The UI is working perfectly ,it only requires some Listeners. I want if possible to use only BottomNavigationView.

Ad

Answer

This is how you set the listener:

    val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.btn1 -> {
                // put your code here
                [email protected] true
            }
            R.id.btn2 -> {
                // put your code here
                [email protected] true
            }
            R.id.btn3 -> {
                // put your code here
                [email protected] true
            }
            R.id.btn4 -> {
                // put your code here
                [email protected] true
            }
        }
        false
    }

menuBar.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

If menuBar is inside your activity's layout it does not need initialization.
If not, you must use findViewById() to initialize it.

Ad
source: stackoverflow.com
Ad