First of all, create a new Android application AndroidOptionMenu.
Create a folder, "menu", under /res
Create a menu.xml under /res/menu
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_help"
android:title="Help" />
<item android:id="@+id/menu_OK"
android:title="OK" />
<item android:id="@+id/menu_Cancel"
android:title="Cancel" />
</menu>
Modify AndroidOptionMenu.java to implement the methods onCreateOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item):
package com.exercise.AndroidOptionMenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidOptionMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater myMenuInflater = getMenuInflater();
myMenuInflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case(R.id.menu_OK):
Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
break;
case(R.id.menu_help):
Toast.makeText(this, "Help", Toast.LENGTH_LONG).show();
break;
case(R.id.menu_Cancel):
Toast.makeText(this, "Cancel", Toast.LENGTH_LONG).show();
break;
}
return true;
}
}
Oh..very useful for me . thanks a lot.
ReplyDelete