Monday, 14 January 2013

Options Menu Example in Android

Project Structure :


 

activity_main.xml

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

    <TextView
        android:id="@+id/layout_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>


 option_menu.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/save_menu_item"
          android:icon="@drawable/icon_save"
          android:title="Save" />

    <item android:id="@+id/delete_menu_item"
          android:icon="@drawable/icon_delete"
          android:title="Delete" /> 
</menu>


 MainActivity.java

 package com.rajeshvijaykumar.option_menus_example;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
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) {
        getMenuInflater().inflate(R.menu.option_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.save_menu_item:
            Toast.makeText(MainActivity.this,
                    "You have Pressed Save Menu Item", Toast.LENGTH_SHORT)
                    .show();
            return true;
        case R.id.delete_menu_item:
            Toast.makeText(MainActivity.this,
                    "You have Pressed Delete Menu Item", Toast.LENGTH_SHORT)
                    .show();
            return true;
        }
        return false;
    }
}

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rajeshvijaykumar.option_menus_example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.rajeshvijaykumar.option_menus_example.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Output :





Source Code : Download this example Here





No comments:

Post a Comment