Android Menu suggest change

Creating a Menu in an Activity

To define your own menu, create an XML file inside your project’s res/menu/ directory and build the menu with the following elements:

Step 1:

Create your own xml file as the following:

In res/menu/main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/aboutMenu"
        android:title="About" />
    <item
        android:id="@+id/helpMenu"
        android:title="Help" />
    <item
        android:id="@+id/signOutMenu"
        android:title="Sign Out" />
</menu>

Step 2:

To specify the options menu, override onCreateOptionsMenu() in your activity.

In this method, you can inflate your menu resource (defined in your XML file i.e., res/menu/main_menu.xml)

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

When the user selects an item from the options menu, the system calls your activity’s overridden onOptionsItemSelected() method.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
       case R.id.aboutMenu:
           Log.d(TAG, "Clicked on About!");
           // Code for About goes here
           return true;
       case R.id.helpMenu:
           Log.d(TAG, "Clicked on Help!");
           // Code for Help goes here
           return true;
       case R.id.signOutMenu:
           Log.d(TAG, "Clicked on Sign Out!");
           // SignOut method call goes here
           return true;
       default:
           return super.onOptionsItemSelected(item);
   }
}

Wrapping up!

Your Activity code should look like below:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "mytag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.aboutMenu:
                Log.d(TAG, "Clicked on About!");
                // Code for About goes here
                return true;
            case R.id.helpMenu:
                Log.d(TAG, "Clicked on Help!");
                // Code for Help goes here
                return true;
            case R.id.signOutMenu:
                Log.d(TAG, "User signed out");
                // SignOut method call goes here
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

Screenshot of how your own Menu looks:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents