How to get an MenuItem by id

menu item id
onprepareoptionsmenu
android get menuitem id
get image view from menu item android
get menu item view android
android navigationview get menu item
menuitem android
menu item id android

I have my menuItem on my res/menu/student_marks.xml file:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".StudentMarks" >
    <item android:id="@+id/action_selected_year"
        android:title="@string/action_selected_year"
        android:showAsAction="withText|ifRoom"
        style="@style/AppTheme" />
</menu>

Now i need for this item to set a title.in a specific part of my app. I can work with a specific item in this method:

onOptionsItemSelected(MenuItem item)

the problem is that i need the item 'action_selected_year' without of this method but in another part of my program. I don't have idea how to get it.

Menu optionsMenu;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu);
       //  store the menu to var when creating options menu
       optionsMenu = menu;
    }

example: change icon on first menuItem (optionsMenu should be != null)

optionsMenu.getItem(0).setIcon(getResources()
    .getDrawable(R.drawable.ic_action_green));

How to access an Android MenuItem from a Java Activity or , id.menuItemPinQuote); } Once you have a reference to the MenuItem in your Java code, you� In MFC there is a func to get the Command-ID if you know the Menu and Position but no func gives you the Menu and the Menupos from the CommandID :- This function will find the first menu item with the given command id looping through the submenu until it is found.

Menu optionsMenu;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.main, menu);
   //  store the menu to var when creating options menu
   optionsMenu = menu;
}

And to get a menu item:

MenuItem item = optionsMenu.findItem(R.id. action_selected_year);

Get menu item by Id? — Xamarin Community Forums, I have the following code: <?xml version="1.0" encoding="utf-8" ?> <menu xmlns: android="http://schemas.android.com/apk/res/android"� then you can find your menu item by id like this. NavigationView navigationView = findViewById(R.id.navigationView); Menu menu = navigationView.getMenu(); MenuItem menuItem = menu.findItem(R.id.your_menu_item_id);

If your menu inside NavigationView example:

<com.google.android.material.navigation.NavigationView 
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/menu_layout" />

then you can find your menu item by id like this

NavigationView navigationView = findViewById(R.id.navigationView);
Menu menu = navigationView.getMenu();
MenuItem menuItem = menu.findItem(R.id.your_menu_item_id);

MenuItem, Return the group identifier that this menu item is part of. abstract Drawable � getIcon(). Returns the icon for this item as a Drawable (getting it� The MenuItem handles the MouseDown event, so if you attach an event handler to MouseDown, your handler will never be called.To add your own handler, subscribe to the PreviewMouseDown event or subscribe to MouseDown by calling AddHandler(RoutedEvent, Delegate, Boolean) with handledEventsToo set to true.

How do you get the current-menu-item ID?, A little late perhaps, but there is one more way of doing it: $menu = wp_get_nav_menu_items($menu_id,array( 'posts_per_page' => -1, 'meta_key'� How to get menu item id on action bar when other menu item clicked. Ask Question Asked 6 years, 8 months ago. Active 5 years, 10 months ago. Viewed 24k times

Get menu item by ID in Joomla 3, Not a big difference so far for what you are after $mainframe = JFactory:: getApplication(); $menu = $mainframe->getMenu(); // Use this if you want to get all� public boolean onOptionsItemSelected(MenuItem item) { // the id is of type int int someId = item.getItemId(); // can use an if() or switch() statement to check if id is selected //a Toast message can be used to show item is selected }

Finding Nav Menu Item I.D, There's at least 2 ways to find the i.d for each menu item in your navigation menu. You can also go to Appearance > Menus and hover your mouse pointer over� public boolean onCreateOptionsMenu(Menu menu) { // Inflate your main_menu into the menu getMenuInflater().inflate(R.menu.main_menu, menu); // Find the menuItem to add your SubMenu MenuItem myMenuItem = menu.findItem(R.id.my_menu_item); // Inflating the sub_menu menu this way, will add its menu items // to the empty SubMenu you created in the

Comments
  • Perfect, save the reference of the menu inside of the activity variable, allow to me a perfect control on the menu object. Thank you.
  • That does not get the menu item by ID. That simply returns the first menu item.
  • You can call findItem() and pass the id you're looking for, instead of calling getItem(0).
  • This appears to be a duplicate of another answer and is at risk of being down voted. To avoid that, either explain why it is different or delete it.