|
|
Select a menu option and see a toast popup. The Options Menu is created programmatically with menu.add(..) statements in the onCreateOptions(..) callback. Callback, onOptionsItemSelected(..) executes the individuals toasts and leaves a TextView message.
package com.brainyideas.dev.poc.omstpu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class OptionsMenuApp extends Activity {
public static final int MENU_1 = Menu.FIRST+1;
public static final int MENU_2 = Menu.FIRST+2;
String msg = "Selected Menu Option is ... ";
TextView textView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
textView = new TextView(this);
textView.setText(msg);
setContentView(textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_1, Menu.NONE, "Add").setIcon(R.drawable.menu_home);
menu.add(Menu.NONE, MENU_2, Menu.NONE, "Setup").setIcon(R.drawable.menu_gear);
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
String selected;
switch (item.getItemId()) {
case MENU_1:
seeToastAndMsg("home");
return(true);
case MENU_2:
seeToastAndMsg("setup");
return(true);
}
return(super.onOptionsItemSelected(item));
}
void seeToastAndMsg(String toast) {
textView.setText(msg + toast);
Toast.makeText(
getApplicationContext(),
toast,
Toast.LENGTH_SHORT).show();
}
}