HOW TO CREATE MENU?
Activity_main.xml file
<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"
android:background="@drawable/abc" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#00ff00"/>
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/abc" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#00ff00"/>
</RelativeLayout>
MainActivity.JAva
package com.example.menudemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.item1)
{
tv1.setText("FirstMenuSelected");
}
else if(item.getItemId()==R.id.item2)
{
tv1.setText("SecondMenuSelected");
}
else if(item.getItemId()==R.id.item3)
{
tv1.setText("ThirdMenuSelected");
}
else if(item.getItemId()==R.id.item4)
{
tv1.setText("FourthMenuSelected");
}
else if(item.getItemId()==R.id.item5)
{
tv1.setText("FifthMenuSelected");
}
else
{
Toast.makeText(getApplicationContext(),"Item Other Selected",Toast.LENGTH_LONG).show();
}
return true;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.item1)
{
tv1.setText("FirstMenuSelected");
}
else if(item.getItemId()==R.id.item2)
{
tv1.setText("SecondMenuSelected");
}
else if(item.getItemId()==R.id.item3)
{
tv1.setText("ThirdMenuSelected");
}
else if(item.getItemId()==R.id.item4)
{
tv1.setText("FourthMenuSelected");
}
else if(item.getItemId()==R.id.item5)
{
tv1.setText("FifthMenuSelected");
}
else
{
Toast.makeText(getApplicationContext(),"Item Other Selected",Toast.LENGTH_LONG).show();
}
return true;
}
}
MyMenu.xmlFile
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item1" android:title="first" android:orderInCategory="1"></item>
<item android:id="@+id/item2" android:title="second" android:orderInCategory="2"></item>
<item android:id="@+id/item3" android:title="third" android:orderInCategory="3"></item>
<item android:id="@+id/item4" android:title="Fouth" android:orderInCategory="4"></item>
<item android:id="@+id/item5" android:title="Fifth" android:orderInCategory="5"></item>
</menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item1" android:title="first" android:orderInCategory="1"></item>
<item android:id="@+id/item2" android:title="second" android:orderInCategory="2"></item>
<item android:id="@+id/item3" android:title="third" android:orderInCategory="3"></item>
<item android:id="@+id/item4" android:title="Fouth" android:orderInCategory="4"></item>
<item android:id="@+id/item5" android:title="Fifth" android:orderInCategory="5"></item>
</menu>