Saturday 7 September 2013

Android Options Menu Integration Guide Tutorials With Example

Android Option Menu Android Option Menu
  1. In this tutorial we show you how to implement Option Menu in Android SDK.
  2. Option menu is one of the attractive user interface in Android.
  3. Option menu is basically used to provide some help in application,setting part of your application and provide some edit,delete functionality or also you can provide navigation in same Activity or Another Activity.
  4. There is three types of menu available in android
    1. Option Menu
    2. Context Menu
    3. Submenus
  5. For Implementing Option Menu in Android Application Follow below step by step guideline
  1. Create new Android Project and fill all require details.
  2. Look at your android project structure find out res folder in that create one menu folder and create color.xml for creating menu items which is shown in above image and modify as given code.
     <?xml version="1.0" encoding="utf-8"?>  
     <menu xmlns:android="http://schemas.android.com/apk/res/android">  
           <item   
                android:title="Red"  
                android:orderInCategory="2"  
                android:id="@+id/red"/>  
           <item   
                android:title="Green"  
                android:orderInCategory="1"  
                android:id="@+id/green"/>  
           <item   
                android:title="Blue"  
                android:orderInCategory="3"  
                android:id="@+id/blue"/>  
           <item   
                android:title="Pink"  
                android:orderInCategory="4"  
                android:id="@+id/pink"/>  
                  
           <item   
                android:title="Yellow"  
                android:orderInCategory="5"  
                android:id="@+id/yellow"/>  
                   
     </menu>  
       
    
  3. We are created 5 menu items for option menu as display in above image you can also set icon by android:icon property of menu item and set any android drawable resources.
  4. Now open your MenuActivity.java file and type following code. In Following code each items define by ID and it is identify by ID in switch case statement.As per ID selection we will set color of background resources.
     package com.idroid.android.menudemo;  
       
     import android.app.Activity;  
     import android.os.Bundle;  
     import android.view.Menu;  
     import android.view.MenuItem;  
     import android.widget.LinearLayout;  
     import android.widget.TextView;  
       
       
     public class MenuActivity extends Activity  
     {  
          TextView tv;  
          LinearLayout l;  
            
       /** Called when the activity is first created. */  
       @Override  
       public void onCreate(Bundle savedInstanceState)   
       {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
           
         tv=(TextView)findViewById(R.id.txt1);  
         l=(LinearLayout)findViewById(R.id.LL1);  
       }  
         
       @Override  
       public boolean onCreateOptionsMenu(Menu menu)  
       {  
            getMenuInflater().inflate(R.menu.color, menu);  
            return true;  
       }  
         
       @Override  
       public boolean onOptionsItemSelected(MenuItem item)   
       {  
               int color=0;  
               switch(item.getItemId())  
               {  
                    case R.id.red:  
                         color=getResources().getColor(R.color.red);  
                         break;  
                           
                    case R.id.green:  
                         color=getResources().getColor(R.color.green);  
                         break;  
                           
                    case R.id.blue:  
                         color=getResources().getColor(R.color.blue);  
                         break;  
                           
                    case R.id.pink:  
                         color=getResources().getColor(R.color.pink);  
                         break;  
                           
                    case R.id.yellow:  
                         color=getResources().getColor(R.color.yellow);  
                         break;  
                           
                    default:  
                         break;  
               }  
               l.setBackgroundColor(color);  
               return true;  
          }  
         
     }  
    
  5. Finally run your project by right click on project and run as Android Application and on Android Simulator select Menu button to launch Option Menu


Download Source Code :Android_OptionMenu_Demo.zip

No comments:

Post a Comment