Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I inflate an Android options menu and set an item to Enabled=false?

Tags:

android

My XML menu definition sets the item R.id.menu_refresh's enabled state to false. When the app runs the menu item is greyed and disabled. Why is this code in the app not enabling the item?

public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    MenuItem refresh = menu.getItem(R.id.menu_refresh);
    refresh.setEnabled(true);
    return true;
}

What am I missing?

like image 554
Ollie C Avatar asked Feb 16 '11 20:02

Ollie C


People also ask

How do you inflate a menu?

To use the menu in your activity, you need to inflate the menu resource (convert the XML resource into a programmable object) using MenuInflater. inflate() .

Under Which method option menu is inflated?

You use onCreateOptionsMenu() to specify the options menu for an activity. In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback.

How do I disable menu items?

Disabling a MenuItem You can set the value to this property using the setVisible() method. To disable a particular menu item invoke the setVisible() method on its object by passing the boolean value “false”.

What is android option menu?

The options menu is the primary collection of menu items for an activity. It's where you should place actions that have a global impact on the app, such as "Search," "Compose email," and "Settings." See the section about Creating an Options Menu.


2 Answers

Try menu.findItem() instead of getItem(). getItem() takes an index from [0, size) while findItem() takes an id.

like image 156
adamp Avatar answered Oct 24 '22 01:10

adamp


this is what I do in my activity for the menu handling ...

//Android Activity Lifecycle Method
// This is only called once, the first time the options menu is displayed.
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    return true;
}


//Android Activity Lifecycle Method
// Called when a panel's menu is opened by the user.
@Override
public boolean onMenuOpened(int featureId, Menu menu)
{
    MenuItem mnuLogOut = menu.findItem(R.id.main_menu_log_out_id);
    MenuItem mnuLogIn = menu.findItem(R.id.main_menu_log_in_id);
    MenuItem mnuOptions = menu.findItem(R.id.main_menu_options_id);
    MenuItem mnuProfile = menu.findItem(R.id.main_menu_profile_id);


    //set the menu options depending on login status
    if (mBoolLoggedIn == true)
    {
        //show the log out option
        mnuLogOut.setVisible(true);
        mnuLogIn.setVisible(false);

        //show the options selection
        mnuOptions.setVisible(true);

        //show the edit profile selection
        mnuProfile.setVisible(true);
    }
    else
    {
        //show the log in option
        mnuLogOut.setVisible(false);
        mnuLogIn.setVisible(true);

        //hide the options selection
        mnuOptions.setVisible(false);

        //hide the edit profile selection
        mnuProfile.setVisible(false);
    }

    return true;
}


//Android Activity Lifecycle Method
// called whenever an item in your options menu is selected
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle item selection
    switch (item.getItemId())
    {

    case R.id.main_menu_log_in_id:
    {
        ShowLoginUI();
        return true;
    }

    case R.id.main_menu_log_out_id:
    {
        ShowGoodbyeUI();
        return true;
    }

    case R.id.main_menu_options_id:
    {
        ShowOptionsUI();
        return true;
    }

    case R.id.main_menu_profile_id:
    {
        ShowProfileUI();
        return true;
    }

    default:
        return super.onOptionsItemSelected(item);
    }
}

I like this approach because it makes the code nice and modular

like image 25
Someone Somewhere Avatar answered Oct 24 '22 00:10

Someone Somewhere