Android:Options Menu in Android example



             Let’s see how to create options menu for android application. Menus are used in applications to handle some common functionality around the application. For example “Settings” is a common option item found in every application. It’s a very simple code lets go ahead with the example.
              To modify the default menu of applications, we need to modify res\menu\activity_main.xml file.
Menu\activity_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/menu_settings" android:orderInCategory="100"   
    android:showAsAction="never" android:title="@string/menu_settings"/>
    <item android:id="@+id/item1" android:title="Option1"></item>
    <item android:id="@+id/item2" android:title="Option2"></item>
    <item android:id="@+id/item3" android:title="Option3"></item>
    <item android:id="@+id/item4" android:title="Option4"></item>
    <item android:id="@+id/item5" android:title="Option5"></item>
    <item android:id="@+id/item6" android:title="Option6"></item>
</menu>

Default option menu available was ‘Settings’, now I have added 6 more option items to it.

Layout/activity_main.xml

In the layout screen I have just added a textview control as shown below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Compiletimeerror.com : Android Tutorials"
        tools:context=".MainActivity" />

</RelativeLayout>

                Whenever the user selects any option item, applications onOptionsItemSelected() method is invoked which passes Interface MenuItem selected. To identify which item has been selected, we can make use of the identifier getItemId which returns the unique ID for the menu item. This identifier cannot be changed after the menu is created.

               After running the  application press  "Menu" in the emulator to see options menu.

  
MainActivity.java

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

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
     public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.item1:
          Toast.makeText(this, "Option1", Toast.LENGTH_SHORT).show();
          return true;
        case R.id.item2:
          Toast.makeText(this, "Option2", Toast.LENGTH_SHORT).show();
          return true;
        case R.id.item3:
          Toast.makeText(this, "Option3", Toast.LENGTH_SHORT).show();
          return true;           
        case R.id.item4:
          Toast.makeText(this, "Option4", Toast.LENGTH_SHORT).show();
          return true;
        case R.id.item5:
          Toast.makeText(this, "Option5", Toast.LENGTH_SHORT).show();
          return true;
        case R.id.item6:
          Toast.makeText(this, "Option6", Toast.LENGTH_SHORT).show();
          return true;           
        default:
          return super.onOptionsItemSelected(item);
        } 
    } 
}

Have a look at context menu in Android here

Source code of this application:
OptionsMenu.rar

 



Reactions:

1 comment :

  1. android:showAsAction="never" android:title="@string/menu_settings"/>

    didnt display string code in the program ....

    ReplyDelete