Working on Android Menus – UI Widget

Posted on .
Option Menu

Android Menus is one of the best Part in Android Apps . As a Android developer we should use it whenever we need Views which are not so important like “About us”,”Like and share” etc. Lets explore –

Download Code

STEPS – 

1) .  create new project in eclipse IDE  , goto File => New => Android Project . Give your desire Project Name .

2). create menu.xml under Layout folder .

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<!-- Single menu item 
		 Set id, icon and Title for each menu item
	-->
    <item android:id="@+id/menu_bookmark"
    	  android:icon="@drawable/icon_bookmark"
          android:title="Bookmark" />
    <item android:id="@+id/menu_save"
    	  android:icon="@drawable/icon_save"
          android:title="Save" />
    <item android:id="@+id/menu_search"
    	  android:icon="@drawable/icon_search"
          android:title="Search" />
    <item android:id="@+id/menu_share"
    	  android:icon="@drawable/icon_share"
          android:title="Share" />
    <item android:id="@+id/menu_delete"
    	  android:icon="@drawable/icon_delete"
          android:title="Delete" />  
    <item android:id="@+id/menu_preferences"
    	  android:icon="@drawable/icon_preferences"
          android:title="Preferences" /> 
</menu>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Click on Menu to show Menu Items"
    android:textSize="20dip"
    android:textStyle="bold"
    android:layout_margin="20dip"
    />
</LinearLayout>

AndroidMenusActivity.java is your main activity in which you will inflate the menu .

package androidarena.androidmenus;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class AndroidMenusActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    /* Initiating Menu XML file (menu.xml) */
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }
    
    /**
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        
        switch (item.getItemId())
        {
        case R.id.menu_bookmark:
        	// Single menu item is selected do something
        	// Ex: launching new activity/screen or show alert message
            Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_save:
        	Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_search:
        	Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_share:
        	Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_delete:
        	Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_preferences:
        	Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    

}

Final Output

Output

Final Output

 

Now RUN your project and for queries and errors do post happy coding 🙂

One thought on “Working on Android Menus – UI Widget

  1. Pingback: Android Pull To Refresh With Listview,WebView and ScrollView | Android Arena

Leave a Reply

Your email address will not be published. Required fields are marked *