'Can't show/hide menu items on Kotlin - Android Studio

my application has a menu of 3 options, and what I want is that according to a condition that I define, it shows only 2 options, or it shows 3. I have tried all the options that I have found on google and here, but none have worked for me.

My idea is that as soon as the view is created, the menu already shows the options that it can show according to the condition. But as I have been able to achieve it, I added a button to simulate the event.

I would greatly appreciate your help.

Here I attach the code:

1) activity_home.xml (some part)

    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:onClick="ActivarMenus"/>

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"

    app:menu="@menu/home_menu"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" >

2) Aqui el archivo home_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_item"
        android:icon="@drawable/ic_baseline_more_vert_24"
        app:showAsAction="ifRoom"
        tools:ignore="MenuTitle">
        <menu>
            <item
                android:id="@+id/newRegister"
                android:icon="@drawable/ic_baseline_add_24"
                app:showAsAction="always"
                android:visible="true"
                android:title="Nueva Orden Swab" />
            <item
                android:id="@+id/newAnexoo"
                android:icon="@drawable/ic_baseline_add_24"
                app:showAsAction="always"
                android:visible="true"
                android:title="Nuevo Formulario Anexo O" />
            <item
                android:id="@+id/sync"
                android:icon="@drawable/ic_baseline_sync_24"
                app:showAsAction="always"
                android:visible="true"
                android:title="Sincronizar" />
        </menu>
    </item>
</menu>

3) And a part of the HomeActivity.kt

class HomeActivity : ActivityViewBinding<ActivityHomeBinding, HomeVM>() {

private var mainMenu: Menu? = null

private lateinit var spm : SharedPreferencesManager

private val adapter: OrderAdapter by inject()
override fun inflateLayout(layoutInflater: LayoutInflater) =
    ActivityHomeBinding.inflate(layoutInflater)


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_home)
    //MenuMain.findItem(R.id.newAnexoo).isVisible   = true

}

override fun onCreateOptionsMenu(menu: Menu): Boolean {

    val inflater: MenuInflater = menuInflater
    inflater.inflate(R.menu.home_menu, menu)
    mainMenu = menu;
    return true
}

fun ActivarMenus(view: View) {
    mainMenu?.findItem(R.id.newRegister)?.isVisible   = true

}


Solution 1:[1]

You should try this one it might be work.

if you want to change Menu Items at Run time You can use onPrepareOptionsMenu

@Override
public boolean onPrepareOptionsMenu(Menu menu){

    if (//Your condition) {
        menu.findItem(R.id.newRegister).setVisible(true);
    }else {
        menu.findItem(R.id.newRegister).setVisible(false);
    }
    return true;
} 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bhavin Solanki