Option Menu


this show how a simple option menu can be displayed. the next bits of code need to be put in the main activity but not in the onCreate method, this is because they are use as soon as the activity is started anyway.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.exit:             finish();
break;
case R.id.view_in_market:    Intent browserIntent = new Intent(“android.intent.action.VIEW”,
Uri.parse(“market://details?id=com.android.example”));
startActivity(browserIntent);
break;
}
return true;
}

and the xml file for them menu needs to look like this. the order of the menu items changes the location of the buttons in the menu.

<menu xmlns:android=”http://schemas.android.com/apk/res/android”&gt;
<item android:id=”@+id/view_in_market”
android:title=”View In Market”
android:icon=”@drawable/view_in_market”/> // these images i use can be found in framework.apk, they are called different names though.
<item android:id=”@+id/exit”
android:title=”Exit”
android:icon=”@drawable/exit”/>
</menu>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: