I want to create an intent that launches a new action after clicking on a menu item, but I'm not sure how to do it. I read the Android documentation, but my implementation is wrong ... and some recommendations in the right direction will help. I have listed my code below and commented on my problem areas, I think I am calling the wrong method.
package com.jbsoft.SimpleFlashlight; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.*; import android.view.MenuItem.OnMenuItemClickListener; import android.widget.Button; import android.widget.Toast; public class SimpleFlashLightActivity extends Activity { Button GreenButton; // Declare instances of buttons to use later Button BlueButton; private static final int OK_MENU_ITEM = Menu.FIRST; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); BlueButton = (Button) findViewById(R.id.bluebutton); BlueButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //Display msg when user clicks Blue Button showColorChangeMsg(); // Switch Activities on click Intent blueintent = new Intent(SimpleFlashLightActivity.this, BlueFlashLightActivity.class); startActivity(blueintent); } }); //Install listener for second button GreenButton = (Button) findViewById(R.id.greenbutton); GreenButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Display msg when user clicks Green Button showColorChangeMsg(); Intent greenintent = new Intent(SimpleFlashLightActivity.this, GreenFlashLightActivty.class); startActivity(greenintent); } }); ; /**************************************************************************************/ // Method Declarations // THIS IS WHERE I'M HAVING A PROBLEM MenuItem AddColorButton = (MenuItem)findViewById(R.id.menu_insert); boolean onOptionsItemSelected(AddColorButton) { Intent intent = new Intent(SimpleFlashLightActivity.this, BlueFlashLightActivity.class); startActivity(intent); return true; ; }; /****************************************************************************************/ } private void showColorChangeMsg() { Toast msgtoast = Toast.makeText(this.getBaseContext(), "SWITCH COLOR!", Toast.LENGTH_LONG); msgtoast.show(); } private void showMsg(String msg) { Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG); toast.show(); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater mi = getMenuInflater(); mi.inflate(R.menu.list_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case OK_MENU_ITEM: showMsg("OK"); break; } return super.onOptionsItemSelected(item); } }
android event-handling menuitem onitemclicklistener
Jade Byfield Sep 20 '11 at 4:01 2011-09-20 04:01
source share