In this post, I show how to create a simple Menu for your device.
Create a sub folder "menu" in "res" folder in your project. Then create a new xml file in your menu folder with the name "menu.xml".
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item1" android:title="New"></item>
<item android:id="@+id/item2" android:title="Save"></item>
<item android:id="@+id/item3" android:title="Delete"></item>
</menu>
MenuActivity.java
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 MenuActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// onCreateOptionsMenu() method initalize the contents of Activity's standard options menu.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
//onOptionsItemSelected() method is called whenever an item in our options menu is selected.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) //getItemId returns the id for this menu item.
{
case R.id.item1:
Toast.makeText(this, "You pressed New", Toast.LENGTH_LONG).show();
break;
case R.id.item2:
Toast.makeText(this, "You pressed Save", Toast.LENGTH_LONG).show();
break;
case R.id.item3:
Toast.makeText(this, "You pressed Delete", Toast.LENGTH_LONG).show();
break;
}
return true;
}
}
Run your project and you will see the output as follows.
Create a sub folder "menu" in "res" folder in your project. Then create a new xml file in your menu folder with the name "menu.xml".
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item1" android:title="New"></item>
<item android:id="@+id/item2" android:title="Save"></item>
<item android:id="@+id/item3" android:title="Delete"></item>
</menu>
MenuActivity.java
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 MenuActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// onCreateOptionsMenu() method initalize the contents of Activity's standard options menu.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
//onOptionsItemSelected() method is called whenever an item in our options menu is selected.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) //getItemId returns the id for this menu item.
{
case R.id.item1:
Toast.makeText(this, "You pressed New", Toast.LENGTH_LONG).show();
break;
case R.id.item2:
Toast.makeText(this, "You pressed Save", Toast.LENGTH_LONG).show();
break;
case R.id.item3:
Toast.makeText(this, "You pressed Delete", Toast.LENGTH_LONG).show();
break;
}
return true;
}
}
Run your project and you will see the output as follows.
No comments:
Post a Comment