Menu

Options Menu:

The Options Menu normally floated in the upper region of any GUI as the app bar and contains options relative to the context.

It is mostly utilized for gestures like context switch, search, or anything that is related to the app it is attached to.

Creating an Options Menu:

To create an Options Menu, override the onCreateOptionsMenu method in your activity or fragment.

                  
                    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.options_menu, menu);
    return true;
}
                  
                

Defining Menu Items:

Define menu items in the res/menu/options_menu.xml file.

Example (res/menu/options_menu.xml):

                  
                    <menu xmlns:android="http://schemas.android.com/apk/res/android">
                        <item
                            android:id="@+id/action_search"
                            android:title="Search"
                            android:icon="@android:drawable/ic_menu_search"
                            app:showAsAction="ifRoom" />
                        <item
                            android:id="@+id/action_settings"
                            android:title="Settings"
                            app:showAsAction="never" />
                    </menu>                    
                  
                

Handling Menu Item Clicks:

Handle item clicks in the onOptionsItemSelected method.

                  
                    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_search:
            // Handle search action
            return true;
        case R.id.action_settings:
            // Handle settings action
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}                   
                  
                

Context Menu:

A Context Menu is a floating menu that appears when the user performs a long press on a view (e.g., ListView item, TextView). It provides context-specific options.

Registering Views for Context Menu:

Register views for context menu in the onCreate method.

                  
                    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Register a view for context menu
    registerForContextMenu(findViewById(R.id.textView));
}
                  
                

Creating a Context Menu:

Override the onCreateContextMenu method to create a context menu.

                  
                    @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    getMenuInflater().inflate(R.menu.context_menu, menu);
}
                  
                

Defining Context Menu Items:

Define context menu items in the res/menu/context_menu.xml file.

Example (res/menu/context_menu.xml):

                  
                    <menu xmlns:android="http://schemas.android.com/apk/res/android">
                        <item
                            android:id="@+id/context_item_1"
                            android:title="Context Item 1" />
                        <item
                            android:id="@+id/context_item_2"
                            android:title="Context Item 2" />
                    </menu>
                    
                  
                

Handling Context Menu Item Clicks:

Handle item clicks in the onContextItemSelected method.

                  
                    @Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.context_item_1:
            // Handle context item 1
            return true;
        case R.id.context_item_2:
            // Handle context item 2
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}