How to modify a menu item

RonNYCRonNYC Member ✭✭✭

Here's part of my menu axml:

   <item 
    android:id="@+id/payBill"
    android:title="Pay Bill"/>

If during the course of the app's operation I want to change this menu item text to, say:

   <item 
    android:id="@+id/payBill"
    android:title="Paid Bill"/> 

how would I access the menu item?

Tagged:

Best Answer

  • RonNYCRonNYC ✭✭✭
    Accepted Answer

    I figured it out. It was actually pretty easy:

    I have a global variable:

      IMenu myMenu;
    

    And in

      public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.menu1, menu); 
            myMenu = menu;
            return base.OnCreateOptionsMenu(menu);
    
        }
    

    Later on, when the program enters the code to change the menu text:

        IMenuItem item = myMenu.FindItem(Resource.Id.getPaid);
        item.SetTitle("Paid");
    

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Override the OnOptionsItemSelected method in Activity. Detect itemId to change the title using SetTitle method.

    public class MainActivity : AppCompatActivity
    {
        ...
        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.mainMenu, menu);
    
            return base.OnCreateOptionsMenu(menu);
        }
    
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Resource.Id.menuItem1:
                    {
                        // add your code  
    
                        item.SetTitle("new title");
                        return true;
                    }
                case Resource.Id.menuItem2:
                    {
                        // add your code  
                        return true;
                    }
                case Resource.Id.menuItem3:
                    {
                        // add your code  
                        return true;
                    }
            }
    
            return base.OnOptionsItemSelected(item);
        }
    }
    
  • RonNYCRonNYC Member ✭✭✭
    edited November 2019

    I already have an OnOptionItemSelected procedure which is how the procedure associated with the menu items are invoked. What I need to do is iteratively call this (or something else) to modify the Title of the menu items.

    OnOptionItemSelected looks for a parameter of the form IMenuItem.

    All I have is the Resource.Id.XXX value.

    How do I convert Resource.Id.XXXto IMenuItem?

    Found it (I hope):

       IMenuItem item = (IMenuItem)FindViewById(Resource.Id.getPaid);
       item.SetTitle("Paid");
    

    NOPE: item is null. Not sure why.

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited November 2019

    The OnOptionsItemSelected method has passed an IMenuItem. Just determine if it is the item you want to change. You can use switch to retrieve ** item.ItemId ** or use if statement.

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        switch (item.ItemId)
        {
            case Resource.Id.menuItem1:
                {
                    item.SetTitle("new title");
                    return true;
                }
            ...
        }
    
        //if statement
        //if (item.ItemId == Resource.Id.menuItem1)
        //{
        //    item.SetTitle("new title");
        //}
    
        return base.OnOptionsItemSelected(item);
    }
    

    Check the Tutorial: https://www.c-sharpcorner.com/article/menu-in-xamarin-android/

  • RonNYCRonNYC Member ✭✭✭

    I don't think your solution will work for me. When I click the Menu link in the app, all the menu items appear (which is normal). I want to modify them before they appear.

  • RonNYCRonNYC Member ✭✭✭
    Accepted Answer

    I figured it out. It was actually pretty easy:

    I have a global variable:

      IMenu myMenu;
    

    And in

      public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.menu1, menu); 
            myMenu = menu;
            return base.OnCreateOptionsMenu(menu);
    
        }
    

    Later on, when the program enters the code to change the menu text:

        IMenuItem item = myMenu.FindItem(Resource.Id.getPaid);
        item.SetTitle("Paid");
    
  • JarvanJarvan Member, Xamarin Team Xamurai

    If you have solved the issue, please mark the helpful solution to the answer. It'll help others who face the similar problem.

Sign In or Register to comment.