We will create a
menu button for the Android 2.2 Froyo in this example. You can also develop for
the Android 2.2 Froyo and you can use this menu at Android 2.3 and lower
versions of the android properly.
After the android
3.0, the menu button is deprecated but many people using android now also have
lower versions.
1 ) Context menu
: When you click on a link as long press. You will see some choices on the
screen this is the context menu.
2 ) Pop up menu :
If you see in a faded screen as list this will be the pop up menu.
DEFINING A MENU
IN XML
You should define
your menu items at the xml as developer-android.com says. You can also create
your list in your java code. You can then get this xml as Menu object with
inflating.
You can use your res/menu folders for this xml. Create a new xml named as customize_menu.xml
Example xml is
such below :
<?xml version="1.0"
encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_settings1"
android:title="@string/menu_settings"
android:checkable="true"
android:menuCategory="container"
android:titleCondensed="@string/app_name"
/>
<item
android:id="@+id/menu_settings2"
android:title="@string/menu_settings"
android:checkable="true"
android:menuCategory="container"
android:titleCondensed="@string/app_name"
/>
<item
android:id="@+id/menu_settings3"
android:title="@string/menu_settings"
android:checkable="true"
android:menuCategory="container"
android:titleCondensed="@string/app_name"
/>
</menu>
And also in your
activity class you can code as
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.customize_menu, menu);
return true;
}
For more, you can change your list order with the attribute
of
android:orderInCategory="Number"
for example if you
define your android:orderInCategory in one item as 100 it will be in the 100th
order.
You can also change your contents of the menu items programmatically.
For example changing the all of the menu items titles that helped me http://stackoverflow.com/questions/6150080/set-a-menu-item-as-checked-from-code
After you run this application you must get this screen as in Picture 1
Picture 1
package com.example.menuexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tV = (TextView) findViewById(R.id.can);
tV.setText("Menu Example");
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_settings1).setTitleCondensed("menu1");
menu.findItem(R.id.menu_settings2).setTitleCondensed("menu2");
menu.findItem(R.id.menu_settings3).setTitleCondensed("menu3");
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.customize_menu, menu);
return true;
}
}
This is the java codes related with menu. onPrepareOptionsMenu gives you the opportunity to manipulate the id based resources. You can not manipulate the menu in onCreateOptionsMenu because inflater has not been finished yet so you would crash with the null pointers.
Also there is super tutorial that is suitable for advance customization