014 Java Android Program to Demonstrate the Menu Application
Posted by Superadmin on December 12 2015 14:23:04
This Android Java Program lets you create an Activity to create a Menu in an Application.
Here is source code of the Program to create an Activity to create a Menu in an Application. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.


package com.example.create_menu1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

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) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Xml

Main



<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="A Menu Activity"
android:textSize="30dp"
android:layout_marginTop="120dp"
android:gravity="center_vertical|center_horizontal" />

</RelativeLayout>
Menu


<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/Menu_label1"
android:alphabeticShortcut="1"
android:title="Menu_label"/>
<item
android:id="@+id/Menu_label2"
android:alphabeticShortcut="2"
android:title="Menu_label2"/>

</menu>