
What is OptionMenu ?
Option menu is a primary menu in android.it is primary collection of menu items for an activity. option menu used Like settings,search etc.Mostly android use to option menu is a toolbar top right of three dots.
option menu is a inflate by the inflate() Method called and in this method of MenuInflater class.
Option menu item need to perform event handling item to onOptionsItemSelected() you need to override in this method of activity class file.
Implementation of OptionMenu
Create activity_main.xml File
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_pass_data_activitity" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#cfdb28" tools:context="com.bhaumik.programmingstudy.MainActivitity"> </RelativeLayout> |
Create Menu Resource xml file to res/menu directory to create new xml file.For Example I have create menu example file and name given to option_menu.xml
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_search" android:title="Search">< /item> <item android:id="@+id/menu_share" android:title="Share">< /item> <item android:id="@+id/menu_about" android:title="About">< /item> </menu> |
Create MainActivity.java file
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 |
package com.bhaumik.programmingstudy; import android.support.v7.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.option_menu,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_search: Toast.makeText(getApplicationContext(),"Search Selected",Toast.LENGTH_LONG).show(); return true; case R.id.menu_share: Toast.makeText(getApplicationContext(),"Share Selected",Toast.LENGTH_LONG).show(); return true; case R.id.menu_about: Toast.makeText(getApplicationContext(),"About Selected",Toast.LENGTH_LONG).show(); return true; default: return super.onOptionsItemSelected(item); } } } |
Now above the code is create option menu to create res/menu directory to option_menu.xml file create and insert three item in search,share and about items.now in this menu file inflate on onCreateOptionMenu(Menu menu) method. now when the user press this menu to called onOptionItemSelected(MenuItem item) Method called. in this method use to switch case statement to maintain case when user click menu item to display message.