How to disable an item from a menu in kotlin

How do you disable an item within a menu in kotlin, such that the item is not visible?

My main activity contains:

    <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/topAppBar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="@string/page_title"
                app:menu="@menu/menu_main"
                app:navigationIcon="@drawable/ic_menu_24"
                android:background="@color/autumn_dark_1"
                />

        </com.google.android.material.appbar.AppBarLayout>

And within the menu_main.xml contains:

<menu 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:id="@+id/menu_bar"
    tools:context="com.example.alphamind.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/delete_all"
        android:orderInCategory="100"
        android:title="@string/delete_all"
        app:showAsAction="never" />
    <item
        android:id="@+id/delete_one"
        android:orderInCategory="100"
        android:title="@string/delete_one"
        app:showAsAction="never" />
    <item
        android:id="@+id/save_log"
        android:icon="@drawable/ic_baseline_add_box_24"
        app:showAsAction="always"
        android:title="Save">
    </item>
    <item
        android:id="@+id/calendar_view"
        android:icon="@drawable/ic_baseline_calendar_today_24"
        app:showAsAction="always"
        android:title="Calendar">
    </item>
    <item
        android:id="@+id/charts"
        android:icon="@drawable/ic_baseline_bar_chart_24"
        app:showAsAction="always"
        android:title="Charts">
    </item>
</menu>

In the MainActivity.kt I have:

        lateinit var topAppBar: MaterialToolbar
        topAppBar = findViewById<MaterialToolbar>(R.id.topAppBar)
        topAppBar.setOnMenuItemClickListener { menuItem ->
            when (menuItem.itemId) {
                R.id.save_log -> {
                    // Handle favorite icon press
                    true
                }
                R.id.delete_all -> {
//                    deleteAllObjectsInRealm()
                    true
                }
                R.id.delete_one -> {
                    deleteActivity(selectionList)
                    true
                }
                R.id.calendar_view -> {
                    displayCalenderView()
                    true
                }
                else -> false
            }
        }

How do I disable save_log item programatically? Such that in the MainActivity, it is not visible?

I tried to do this val test = topAppBar.findViewById<Menu>(R.id.save_log) but the linter tells me:

Type argument is not within its bounds.
Expected:
View!
Found:
Menu!

Answer

You can retrieve a MenuItem from your Toolbar by getting it from the Toolbar‘s menu:

val menuItem = topAppBar.menu.findItem(R.id.save_log)
menuItem.visible = false