ANDROID OPTION MENU

Menus are common user component in many types of application.Menu can contains menu items.we can use it for simple options such as search,help,save etc.
 First of all we should need a android resource file that type of menu and root element is menu for our application.

Create a menu resource file

We are inflating the menu by calling the inflate() method of MenuInflater class.Let’s see the simple option menu example code that include two menu items.The file that we create in above is in res/menu directory.so you can create items in that file as below.

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1" android:icon="@drawable/ic_border_color_black_24dp" android:title="Edit"/>
<item android:id="@+id/item2" android:icon="@drawable/ic_archive_black_24dp" android:title="Save"/>
</menu>

we can include icon,title for our option item through android:icon,android:title attributes.

Then we have to implement some override methods for Activity class that display the content of menu.xml file and perfom the event handling on clicking the menu items. Example code of that class is given below.

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1" android:icon="@drawable/ic_border_color_black_24dp" android:title="Edit"/>
<item android:id="@+id/item2" android:icon="@drawable/ic_archive_black_24dp" android:title="Save"/>
</menu>

we can include icon,title for our option item through android:icon,android:title attributes.

Then we have to implement some override methods for Activity class that display the content of menu.xml file and perfom the event handling on clicking the menu items. Example code of that class is given below.

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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menuuu, menu);//Menu Resource, Menu        return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
return true;

default:
return super.onOptionsItemSelected(item);
}
}
}
/**OnCreateOptionMenu(Menu) method inflate the menu and it adds items to the actionbar if it is present.
OnOptionItemSelected(MenuItem) method do event handling of click menu items **/

So we can see a output like that when u run app on your emulator.

Option menu