This message was deleted.
# android
s
This message was deleted.
stackoverflow 2
m
Have you tried this way ?
Copy code
override fun void onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.main_menu_buttons,menu)
        super.onCreateOptionsMenu(menu, inflater)
    }
You have try this inside the Fragment you want to show the menu option
j
should I try using
setHasOptionsMenu(true)
inside my fragment?
m
Yes. also you need to override the onCreateOptionsMenu to add the menu inside your fragment also.
j
I want to hide this button from the fragment
and then on some fragments I want to show it
I just want to show the button on certain specific fragments
Im using single activity pattern
m
I see Can you try this in the fragment you want to hide ?
Copy code
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
    super.onCreateOptionsMenu(menu, inflater)
    menu?.findItem(R.id.menu_id)?.isVisible = false 
}
j
that did not work
here is my toolbar set-up:
Copy code
<?xml version="1.0" encoding="utf-8"?>
<layout 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>">

    <data>
        <import type="android.view.View" />

        <variable
            name="toolbarViewConfig"
            type="ferm.jonny.willow.models.ToolbarViewConfig" />
        <variable
            name="mainActivity"
            type="ferm.jonny.willow.MainActivity" />
    </data>

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer"
        MenuDrawerVisibility="@{toolbarViewConfig.isMenuVisible}"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:context=".MainActivity">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:background="@color/white"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{toolbarViewConfig.title}"
                    android:textSize="18dp"
                    android:textStyle="bold"
                    android:layout_gravity="center"
                    android:id="@+id/toolbar_title" />

            </androidx.appcompat.widget.Toolbar>

            <fragment
                android:id="@+id/nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbar"
                app:navGraph="@navigation/nav_graph" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:clipToPadding="false"
            app:menu="@menu/main_menu"
            />

    </androidx.drawerlayout.widget.DrawerLayout>
</layout>
why do i get indexoutofboundsexception on this? I get it when I try to retrieve my getItem from the menu..
Copy code
override fun onCreateOptionsMenu(menu: Menu?): Boolean {

        menuInflater.inflate(R.menu.main_menu_buttons,menu)
        //val menu = findViewById<NavigationView>(R.id.navigation_view).menu
        //menu.getItem(R.id.action_goOffline).isVisible = cancelButtonIsVisible
        menu?.getItem(R.id.action_goOffline)?.isVisible = false
        return true
    }
nvm i had to use finditem instead of getitem