OptionsMenu is not working


Ask by : user1786113 November 04, 2012 07:27

I am trying make an OptionsMenu, this is my code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}


public boolean onOptionsItemSelect(MenuItem item){

    switch (item.getItemId()){
    case R.id.Guardar:
        Toast.makeText(DrawFunny.this, "Guardar",Toast.LENGTH_LONG).show();

        return true;
    case R.id.Finish:
        return true;

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

Menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    <group android:id="@+id/group1">
        <item android:id="@+id/Guardar" android:title="Guardar"></item>
        <item android:id="@+id/Finish" android:title="Terminar"></item>
    </group>
</menu>

This menu is displayed on the screen but when items are selected, it does nothing.

View original question

Answer by : user1690588November 04, 2012 06:03

While try your code I found some missing lines, I correct it then it work for me. So replace your code with mine or correct yours

XML

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/group1">
    <item android:id="@+id/Guardar" android:title="Guardar"></item>
    <item android:id="@+id/Finish" android:title="Terminar"></item>
    </group>
</menu>

Activity

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_disk_shooter, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.i(TAG, "onOptionsItemSelected()");
        switch (item.getItemId()){
        case R.id.Guardar:
            Toast.makeText(DiskShooterActivity.this, "Guardar",Toast.LENGTH_LONG).show();

            return true;
        case R.id.Finish:
            return true;

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

Try it..,.

View original answer