Create an Application for Demonstration of Android Option Menu & Context Menu.

MainActivity.java :
package com.example.extraprac18;

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 {

    private static final int LENGTH_LONG = 0;

            @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
   
    @Override
    public boolean onOptionsItemSelected(MenuItem Item)
    {
           
            switch (Item.getItemId() )
            {
                                    case R.id.open:
                                    Toast.makeText(this,"OPEN IS SELECTED",LENGTH_LONG).show();
                                    return true;

                                    case R.id.save:
                                    Toast.makeText(this,"SAVE IS SELECTED",LENGTH_LONG).show();
                                    return true;
                       
                                    case R.id.close:
                                    Toast.makeText(this,"CLOSE IS SELECTED",LENGTH_LONG).show();
                                    return true;
                                   
                                    default:
                                    return super.onOptionsItemSelected(Item);
                        }
                       
                       
    }
   
}

Activity_main.xml :
<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="@string/ExtraPrac18" />

</RelativeLayout>

Menu.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/open" android:title="OPEN"></item>
    <item android:id="@+id/save" android:title="SAVE"></item>
    <item android:id="@+id/close" android:title="CLOSE"></item>
   

</menu>

Post a Comment

1 Comments