Android-Menus
Android Options Menu the main menus on Android are menus. They can be used for settings, searches, item deletion, and other things.
The menu should be defined in a separate XML file that is used in our application in line with our specifications.
How to create a menu directory and menu resource file:
Under the res folder just right-click on res folder and navigate to res->New->Android Resource Directory. Give resource directory name as menu and resource type also menu.
Now to create xml resource file simply right-click on menu folder and navigate to New->Menu Resource File. Give the name of the file as menu_main.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.menu.MainActivity"> <item android:id="@+id/search_item" android:title="Search" /> <item android:id="@+id/upload_item" android:title="Upload" /> <item android:id="@+id/copy_item" android:title="Copy" /> <item android:id="@+id/print_item" android:title="Print" /> <item android:id="@+id/share_item" android:title="Share" /> <item android:id="@+id/bookmark_item" android:title="BookMark" /> </menu> |
- <menu>: It serves as the XML file’s root element and defines the Menu. It can also hold several items.
- <item>: It is utilized to make a single menu item. In order to construct a submenu, it also has nested menu elements.
- <group>: In order for the menu items to share features like active state and visibility, categorization of the menu items is optional and invisible for <item> components.
Android Different Types of Menus:
- Android Options Menu
- Android Context Menu
- Android Popup Menu
Android Options: In an Android application, the menu is the main group of menu items and is helpful for activities that have an overall effect on the entire program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.example.menu; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show(); switch (item.getItemId()) { case R.id.search_item: // do your code return true; case R.id.upload_item: // do your code return true; case R.id.copy_item: // do your code return true; case R.id.print_item: // do your code return true; case R.id.share_item: // do your code return true; case R.id.bookmark_item: // do your code return true; default: return super.onOptionsItemSelected(item); } } } |
Android Popup Menu: The vertical list of items in the Android Popup Menu exposes the view from which the menu was invoked and is handy for providing an abundance of actions related to certain content.
Android Context Menu: Android Context Menus are helpful for elements that have an impact on the currently selected content or context frame and only appear when the user clicks on an element for an extended period of time.