Saturday, April 26, 2014

Creating Option Menus

In this post we will see how to create option menu for the android application. It is the common user Interface that is used almost in all application. The menu is displayed whenever the Menu button is pressed. Generally the Menu contain options that are relevant to current Activity. For Example for a browser it may contain - " New window, Bookmark, refresh, forward, backward etc. "



We can declare our items for option menu in Activity subclass or in Fragment subclass. If the items are declared in both Activity and Fragment, then it is combined in UI. The items of the Activity appears first followed by the items of Fragments.

We should @Override OnCreateOptionMenu, while using option Menu in the activity. The OnCreateOptionMenu returns a Boolean. It should either return true or it should return false. Whenever it returns True the Menu appears and when it is false the menu gets invisible.

In the layout directory create an menu.xml file and create the items that you want to put with specific id. You to even add icon picture using android:icon="@drawable/ "

Below is the code for creating items for the Option menu.

 <?xml version="1.0" encoding="utf-8"?>  
 <menu xmlns:android="http://schemas.android.com/apk/res/android">  
   <item   
     android:id="@+id/search"  
     android:icon="@drawable/search"  
      android:title="Search" />  
   <item  
     android:id="@+id/home"  
     android:icon="@drawable/home"  
     android:title="Home" />  
    <item  
     android:id="@+id/refresh"  
     android:icon="@drawable/refresh"  
     android:title="Refresh" />  
    <item  
     android:id="@+id/backward"  
     android:icon="@drawable/backward"  
     android:title="Backward" />  
     <item  
     android:id="@+id/forward"  
     android:icon="@drawable/forward"  
     android:title="Forward" />  
      <item  
     android:id="@+id/history"  
     android:icon="@drawable/history"  
     android:title="History" />  
      </menu>  


You  can keep you default .xml file as it is.

The java code for creating option menu.

 public class MainActivity extends Activity {  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
      }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
   MenuInflater menuInflater = getMenuInflater();  
   menuInflater.inflate(R.layout.menu, menu);  
     return true;  
      }  
       @Override  
        public boolean onOptionsItemSelected(MenuItem item)  
        {  
          switch (item.getItemId())  
          {  
          case R.id.search:  
            Toast.makeText(MainActivity.this, "search is selected ", Toast.LENGTH_SHORT).show();  
            return true;  
          case R.id.home:  
               Toast.makeText(MainActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();  
            return true;  
          case R.id.refresh:  
               Toast.makeText(MainActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();  
            return true;  
          case R.id.backward:  
               Toast.makeText(MainActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();  
            return true;  
          case R.id.forward:  
               Toast.makeText(MainActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();  
            return true;  
          case R.id.history:  
               Toast.makeText(MainActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();  
            return true;  
          default:  
            return super.onOptionsItemSelected(item);  
          }  
        }  
 }  


Whenever we will select an item from the option menu the system call onOptionItemSelected() method from the activity. This method passes menuItem selected. we can identify the item by calling getItemId(). Each item has a unique Id which is defined in menu.xml file (using android:id attribute).
This Id can be matched with the menuItem to perform specific action.






The link for the code :-https://www.dropbox.com/s/vlsgonlsuv8psw9/menu1.rar

In the Next post we see how create a simple Browser.



No comments:

Post a Comment