Menu is use to select single option from number of listed choice. In the android three type of menu used to select option. This are-
- Option Menu - It will be shown when user click on three dots(Top right corner).
- Context Menu - It will be shown when user long click on registered object.
- Pop up Menu - It will be shown when user long click on any object which is registered to open popup menu.
Steps to use Menu in your project.
- Crate a project in android studio.
- Open your main_activity.xml and update it-
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.pankaj.menudemo.MainActivity"> <!--This is an action bar configuration if you want to customization in it then you can here.--> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <!--Main Content--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.pankaj.menudemo.MainActivity"> <!--Button to open context menu--> <Button android:id="@+id/btnContaxtMenu" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Context Menu" /> <!--Button to open popup menu--> <Button android:id="@+id/btnPopupMenu" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="openPopupMenu" android:text="Popup Menu" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout>
- Create a menu file in menu directory(main_menu.xml).
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <!--I am using three element in menu you can update it according to your need.--> <!--In the option menu first item will be show with image icon and other two option will be show in the form of listing.--> <!--In the context and popup menu all three item will be show in listing form.--> <item android:id="@+id/mPointOfAndroid" android:icon="@android:drawable/ic_dialog_info" android:orderInCategory="1" android:title="Point of Android" app:showAsAction="ifRoom" /> <item android:id="@+id/mSettings" android:orderInCategory="2" android:title="Settings" /> <item android:id="@+id/mLogout" android:orderInCategory="3" android:title="Logout" /> </menu>
- Open your MainActivity.JAVA class and update it -
package com.pankaj.menudemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.PopupMenu; import android.support.v7.widget.Toolbar; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { //_________create objects of buttons private Button btnPopupMenu, btnContaxtMenu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //_________create toolbar object Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //_________add toolbar setSupportActionBar(toolbar); //_________initialize buttons btnPopupMenu = (Button) findViewById(R.id.btnPopupMenu); btnContaxtMenu = (Button) findViewById(R.id.btnContaxtMenu); //_________register button for context menu registerForContextMenu(btnContaxtMenu); } //_________override method to create context menu @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //________add title of the menu, if is it. menu.setHeaderTitle("This is a Context Menu"); //_________add icon of the menu, if is it. menu.setHeaderIcon(getResources().getDrawable(R.mipmap.ic_launcher)); //_____________inflate main_menu.xml file to show option in context menu getMenuInflater().inflate(R.menu.menu_main, menu); } //_________override method to process selected context menu option. @Override public boolean onContextItemSelected(MenuItem item) { //_________Message will be shown, When option selected from context menu. Toast.makeText(this, "Context Menu : " + item.getTitle(), Toast.LENGTH_SHORT).show(); return super.onContextItemSelected(item); } //_________override method to create options menu @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. //_____________inflate main_menu.xml file to show option in popup menu getMenuInflater().inflate(R.menu.menu_main, menu); return true; } //_________override method to process selected options menu option. @Override public boolean onOptionsItemSelected(MenuItem item) { //_________Message will be shown, When option selected from options menu. Toast.makeText(this, "Option Menu : " + item.getTitle(), Toast.LENGTH_SHORT).show(); return super.onOptionsItemSelected(item); } //_________override method to create popup menu. public void openPopupMenu(View view) { //_________create popup menu object PopupMenu popupMenu = new PopupMenu(this, btnPopupMenu); //_____________inflate main_menu.xml file to show option in popup menu popupMenu.getMenuInflater().inflate(R.menu.menu_main, popupMenu.getMenu()); //__________set listener to call back on item selection popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { //_________override method to process selected popup menu option. @Override public boolean onMenuItemClick(MenuItem item) { //_________Message will be shown, When option selected from popup menu. Toast.makeText(MainActivity.this, "Popup Menu : " + item.getTitle(), Toast.LENGTH_SHORT).show(); return false; } }); popupMenu.show(); } }
-
Now all the development steps for menu has completed, Please run the application.
- Now click on option menu(three dots on top right corner), this will be open the option menu, then select any option from here. See the result on screen-
- Now click on "Context Menu", this will be open the context menu, then select any option from here. See the result on screen-
- Now click on "Popup Menu", this will be open the popup menu, then select any option from here. See the result on screen-
- Good bye, Thanks to read this blog.