Handling Click Event Menu Item - Android

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); } } 
+108
android event-handling menuitem onitemclicklistener
Sep 20 '11 at 4:01
source share
5 answers

simple code to create a menu.

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } 

simple code for the selected menu

 @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.new_game: newGame(); return true; case R.id.help: showHelp(); return true; default: return super.onOptionsItemSelected(item); } } 

For more details see the link ..

Link1

Link2

+264
Sep 20 '11 at 4:20
source share

Add the following code

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.new_item: Intent i = new Intent(this,SecondActivity.class); this.startActivity(i); return true; default: return super.onOptionsItemSelected(item); } } 
+9
Sep 07 '15 at 10:12
source share

The menu item file looks like

Res / menu / menu_main.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/settings" android:title="Setting" app:showAsAction="never" /> <item android:id="@+id/my_activity" android:title="My Activity" app:showAsAction="always" android:icon="@android:drawable/btn_radio"/> </menu> 

Java code looks like

SIC / MainActivity.java

 @Override public boolean onCreateOptionsMenu(Menu menu) { present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.my_activity) { Intent intent1 = new Intent(this,MyActivity.class); this.startActivity(intent1); return true; } if (id == R.id.settings) { Toast.makeText(this, "Setting", Toast.LENGTH_LONG).show(); return true; } return super.onOptionsItemSelected(item); } 

And add the following code to your AndroidManifest.xml file

 <activity android:name=".MyActivity" android:label="@string/app_name" > </activity> 

Hope this helps you.

+5
04 Sep '15 at 4:44
source share

This code works for me

 @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { // add your action here that you want return true; } else if (id==R.id.login) { // add your action here that you want } return super.onOptionsItemSelected(item); } 
+2
Jan 18 '17 at 7:27
source share

In addition to the parameters specified in your question, it is possible to implement the action directly in your XML file from the menu, for example:

 <item android:id="@+id/OK_MENU_ITEM" android:onClick="iraActivitySobre" /> 

And for your Java (Activity) file, you need to implement an open method with one parameter of type MenuItem, for example:

  private void showMsgDirectMenuXml(MenuItem item) { Toast toast = Toast.makeText(this, "OK", Toast.LENGTH_LONG); toast.show(); } 

NOTE. This method will have a behavior similar to onOptionsItemSelected (MenuItem element)

0
Jun 25 '19 at 1:39 on
source share



All Articles