Day 8 : Option Menu in Android

Introduction

 
Menus are an integral part of any app. It is very rare to see an app without option menus. Likewise, we will try an Options menu in this article with a small demo.
 
Option Menu
 
For this, we will use the inflate() method of the MenuInflater class.
 
Procedure
 
Step 1
 
Before we start we will add a few packages that we will use in this project.
  1. import android.view.Menu;  
  2. import android.view.MenuItem;  
  3. import android.widget.Toast;  
  Step 2
 
Similar to an Activity's XML file, we will code the MENU file that itself is a XML file. And here we add the value of the items.
 
In the new Android SDK Package, Eclipse generates a dummy Menu that you will get in the res/menu.
 
By default, you have a main.xml file that contains the menu-items.
 
We will now add some items to the XML file.
 
Code
  1. <item   
  2.         android:id="@+id/one"  
  3.         android:title="One" />  
  4.       
  5.     <item  
  6.         android:id="@+id/two"  
  7.         android:title="Second"/>  
  8.       
  9.     <item   
  10.         android:id="@+id/third"  
  11.         android:title="Third"/>  
Step 3
 
We have now done the design part, we will now code our Java file. So, get to the MainActivity.java file and ensure that you have this default code:
 
Code
  1. @Override  
  2. public boolean onCreateOptionsMenu(Menu menu) {  
  3.   
  4. // Inflate the menu; this adds items to the action bar if it is present.  
  5. getMenuInflater().inflate(R.menu.main, menu);  
  6. return true;  
  7. }  
It will create your Option menu. And you can see the infalte() method that populates the main.xml file to the Options Menu.
 
Step 4
 
We will now add the click listener property. Since it's not a button, that's why we have various ways to do this. In the Options Menu or any Menu, there is an onOptionItemSelected() method.
 
Code
  1. public boolean onOptionsItemSelected(MenuItem item) {  
  2.           
  3. switch(item.getItemId()){  
  4. case R.id.one:  
  5. Toast.makeText(MainActivity.this, "First is Selected", Toast.LENGTH_LONG).show();  
  6. return false;  
  7.               
  8.               
  9. case R.id.two:  
  10. Toast.makeText(MainActivity.this, "Second is Selected", Toast.LENGTH_LONG).show();;  
  11. return false;  
  12.               
  13.               
  14. case R.id.third:  
  15. Toast.makeText(MainActivity.this, "Third is seleced", Toast.LENGTH_LONG).show();;  
  16. return false;  
  17.               
  18. }  
  19. return false;  
  20.       
  21. }  
Here we are using the switch() way that is a convenient way to judge which option is selected.
 
item.getItemId() will return the ID of the selected item and depending on that, it will popup the toast message.
 
If you have done your code right, then it will look like:
 
step 4    step 4 second
 

Conclusion

 
If you encounter any problem then please refer to Quick fix (Ctrl+1), as it is capable of solving most of the problems or just go to the enclosed project file.


Similar Articles