Android development primer: Creating Options Menu in Android – Part IV

This entry is part 11 of 50 in the series Android Development Primer

This is the last post in the  Creating Options Menu in Android section. I told you in the first post of this section, that the Options Menu layout can also be created using  XML. In this post I am going to show you how.

I will be editing my codes from previous posts. You can create a new project and follow along. I would recommend creating a new project because we will not be changing the default code provided in the MainActivity.java much. Complete Source Code is at the bottom.

  • Navigate to <yourprojectname> -> res -> menu -> main.xml. Open the main.xml file.
  • You can already see one item in it. That is provided by default. You may or may not delete it.
  • Write the following code before the closing menu tag i.e. before </menu>.
    <item
            android:id="@+id/menuitem1"
            android:title="MenuItem1"
            android:icon="@drawable/menuitemicon1" />
    
    <item
            android:id="@+id/menuitem2"
            android:title="MenuItem2"
            android:icon="@drawable/menuitemicon2" />
    
  • Switch over to MainActivity.java and in the onCreateOptionsMenu() method, write the following
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    
  • Save and execute the application on the emulator/device.
  • In order to use the onOptionsItemSelected() method, write the following code in the method
    switch (item.getItemId()) {
    			  case R.id.menuitem1:
    				Toast.makeText(getApplicationContext(), "MenuItem1 selected", Toast.LENGTH_LONG).show();
    			    return true;
    			  case R.id.menuitem2:
    				Toast.makeText(getApplicationContext(), "MenuItem2 selected", Toast.LENGTH_LONG).show();
    				return true;
    		  }
    		  return super.onContextItemSelected(item);
    
  • Remember that unlike the previous post, the cases here will not be 1,2.. but R.id.menuitem1, R.id.menuitem2
  • This is because when we were adding the menu items from java we were assigning it an ItemId. That allowed Android to identify the menu item uniquely. But here the id assigned is menuitem1, menuitem2 etc. Hence we reference them using R.id.menuitem1, R.id.menuitem2 etc.

img4

COMPLETE SOURCE CODE

MainActivity.java

package com.nero.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
		  switch (item.getItemId()) {
			  case R.id.menuitem1:
				Toast.makeText(getApplicationContext(), "MenuItem1 selected", Toast.LENGTH_LONG).show();
			    return true;
			  case R.id.menuitem2:
				Toast.makeText(getApplicationContext(), "MenuItem2 selected", Toast.LENGTH_LONG).show();
				return true;
		  }
		  return super.onContextItemSelected(item);
	}

}

main.xml(<yourprojectname> -> res -> menu -> main.xml)

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menuitem1"
        android:title="MenuItem1"
        android:icon="@drawable/menuitemicon1" />

    <item
        android:id="@+id/menuitem2"
        android:title="MenuItem2"
        android:icon="@drawable/menuitemicon2" />

</menu>
Series Navigation<< Android development primer: Creating Options Menu in Android – Part IIIAndroid development primer: Know the AndroidManifest.xml >>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

41,650 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>