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

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

Till now we have seen how to create and customize our Options Menu. In this post I will show you how to actually make use of the Options Menu to do something i.e. how to handle the clicks on Menu Items.

I will continue with the code from the previous post. Complete Source Code is at the bottom.

  • The function we will be overriding here is, onOptionsItemSelected() from android.app.activity class.
  • We will be using Toast so I would recommend you get a brief idea about what a Toast is and what it looks like from here. I will explain the different components of the Toast syntax in a while.
  • Write the following code below the onCreateOptionsMenu() method or onPrepareOptionsMenu() if you have one.
    public boolean onOptionsItemSelected(MenuItem item) {
    		  switch (item.getItemId()) {
    			  case 1:
    				Toast.makeText(getApplicationContext(), "MenuItem1 selected", Toast.LENGTH_LONG).show();
    			    return true;
    			  case 2:
    				Toast.makeText(getApplicationContext(), "MenuItem2 selected", Toast.LENGTH_LONG).show();
    				return true;
    		  }
    		  return super.onContextItemSelected(item);
    	}
    
  • Save and execute the application in an emulator/device

part3-2   Part3-1

Here’s a little explanation for the Toast syntax.

  • getApplicationContext() : It returns the context for the whole application. It is used in order to get the context tied to the life cycle of the complete application and not some particular activity.
  • <char_sequence_of_your_choice> : This is the text to be displayed in the Toast.
  • Toast.LENGTH_LONG/Toast.LENGTH_SHORT : This defines the time for which the Toast should be visible.
  • show() : Everything to the left of show() is used to create the Toast. Once created it needs to be displayed. The show() method serves this purpose

Here I have shown you how to create a Toast on the click of a menu item. You can extend this to do practically anything you want like showing dialogs, creating notifications, retrieving data from databases etc.

COMPLETE SOURCE CODE

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) {
        menu.add(0, 1, 0, "MenuItem1").setIcon(R.drawable.menuitemicon1);
		menu.add(0, 2, 0, "MenuItem2").setIcon(R.drawable.menuitemicon2);
        return true;
    }

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

}

 

Series Navigation<< Android development primer: Creating Options Menu in Android – Part IIAndroid development primer: Creating Options Menu in Android – Part IV >>

Leave a Reply

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

*

* Copy This Password *

* Type Or Paste Password Here *

41,055 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>