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.[java]
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);
}
[/java] - Save and execute the application in an emulator/device
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
[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) {
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);
}
}
[/java]