Handling a menu click event in Android
I am trying to set up a click event for a popup menu in Android. I created an overidden onOptionsItemSelected()
, but nothing seems to be happening. Here is the popupmenu.xml file:
popupmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/help"
android:title="Help" />
<item android:id="@+id/information"
android:title="Information" />
<item android:id="@+id/services"
android:title="Services" />
<item android:id="@+id/overdose"
android:title="Overdose" />
</menu>
Here is the onOptionsItemSelected()
method:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help:
Toast toast = Toast.makeText(this, "Help Works", Toast.LENGTH_SHORT);
toast.show();
return true;
case R.id.information:
Toast toast2 = Toast.makeText(this, "Information Works", Toast.LENGTH_SHORT);
toast2.show();
return true;
case R.id.services:
Toast toast3 = Toast.makeText(this, "Services Works", Toast.LENGTH_SHORT);
toast3.show();
return true;
case R.id.overdose:
Toast toast4 = Toast.makeText(this, "Services Works", Toast.LENGTH_SHORT);
toast4.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I was under the impression that onOptionsItemSelected()
was called whenever a menu item was selected, and that it didn't require the android:onClick
attribute in the layout for the activity. Is there something I'm missing here? The popup menu works perfectly otherwise.
java android android-studio events menu
add a comment |
I am trying to set up a click event for a popup menu in Android. I created an overidden onOptionsItemSelected()
, but nothing seems to be happening. Here is the popupmenu.xml file:
popupmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/help"
android:title="Help" />
<item android:id="@+id/information"
android:title="Information" />
<item android:id="@+id/services"
android:title="Services" />
<item android:id="@+id/overdose"
android:title="Overdose" />
</menu>
Here is the onOptionsItemSelected()
method:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help:
Toast toast = Toast.makeText(this, "Help Works", Toast.LENGTH_SHORT);
toast.show();
return true;
case R.id.information:
Toast toast2 = Toast.makeText(this, "Information Works", Toast.LENGTH_SHORT);
toast2.show();
return true;
case R.id.services:
Toast toast3 = Toast.makeText(this, "Services Works", Toast.LENGTH_SHORT);
toast3.show();
return true;
case R.id.overdose:
Toast toast4 = Toast.makeText(this, "Services Works", Toast.LENGTH_SHORT);
toast4.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I was under the impression that onOptionsItemSelected()
was called whenever a menu item was selected, and that it didn't require the android:onClick
attribute in the layout for the activity. Is there something I'm missing here? The popup menu works perfectly otherwise.
java android android-studio events menu
Can you post youronCreateOptionsMenu()
– Savin Sharma
Nov 23 '18 at 2:40
@SavinSharma I think he confusedonCreateOptionsMenu()
as the click listener forPopupMenu
– jake
Nov 23 '18 at 2:43
I figured out what I was doing wrong. Thank you for the input anyways!
– Ian
Nov 24 '18 at 0:43
add a comment |
I am trying to set up a click event for a popup menu in Android. I created an overidden onOptionsItemSelected()
, but nothing seems to be happening. Here is the popupmenu.xml file:
popupmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/help"
android:title="Help" />
<item android:id="@+id/information"
android:title="Information" />
<item android:id="@+id/services"
android:title="Services" />
<item android:id="@+id/overdose"
android:title="Overdose" />
</menu>
Here is the onOptionsItemSelected()
method:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help:
Toast toast = Toast.makeText(this, "Help Works", Toast.LENGTH_SHORT);
toast.show();
return true;
case R.id.information:
Toast toast2 = Toast.makeText(this, "Information Works", Toast.LENGTH_SHORT);
toast2.show();
return true;
case R.id.services:
Toast toast3 = Toast.makeText(this, "Services Works", Toast.LENGTH_SHORT);
toast3.show();
return true;
case R.id.overdose:
Toast toast4 = Toast.makeText(this, "Services Works", Toast.LENGTH_SHORT);
toast4.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I was under the impression that onOptionsItemSelected()
was called whenever a menu item was selected, and that it didn't require the android:onClick
attribute in the layout for the activity. Is there something I'm missing here? The popup menu works perfectly otherwise.
java android android-studio events menu
I am trying to set up a click event for a popup menu in Android. I created an overidden onOptionsItemSelected()
, but nothing seems to be happening. Here is the popupmenu.xml file:
popupmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/help"
android:title="Help" />
<item android:id="@+id/information"
android:title="Information" />
<item android:id="@+id/services"
android:title="Services" />
<item android:id="@+id/overdose"
android:title="Overdose" />
</menu>
Here is the onOptionsItemSelected()
method:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help:
Toast toast = Toast.makeText(this, "Help Works", Toast.LENGTH_SHORT);
toast.show();
return true;
case R.id.information:
Toast toast2 = Toast.makeText(this, "Information Works", Toast.LENGTH_SHORT);
toast2.show();
return true;
case R.id.services:
Toast toast3 = Toast.makeText(this, "Services Works", Toast.LENGTH_SHORT);
toast3.show();
return true;
case R.id.overdose:
Toast toast4 = Toast.makeText(this, "Services Works", Toast.LENGTH_SHORT);
toast4.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I was under the impression that onOptionsItemSelected()
was called whenever a menu item was selected, and that it didn't require the android:onClick
attribute in the layout for the activity. Is there something I'm missing here? The popup menu works perfectly otherwise.
java android android-studio events menu
java android android-studio events menu
edited Nov 23 '18 at 3:54
Savin Sharma
428315
428315
asked Nov 23 '18 at 2:17
IanIan
121
121
Can you post youronCreateOptionsMenu()
– Savin Sharma
Nov 23 '18 at 2:40
@SavinSharma I think he confusedonCreateOptionsMenu()
as the click listener forPopupMenu
– jake
Nov 23 '18 at 2:43
I figured out what I was doing wrong. Thank you for the input anyways!
– Ian
Nov 24 '18 at 0:43
add a comment |
Can you post youronCreateOptionsMenu()
– Savin Sharma
Nov 23 '18 at 2:40
@SavinSharma I think he confusedonCreateOptionsMenu()
as the click listener forPopupMenu
– jake
Nov 23 '18 at 2:43
I figured out what I was doing wrong. Thank you for the input anyways!
– Ian
Nov 24 '18 at 0:43
Can you post your
onCreateOptionsMenu()
– Savin Sharma
Nov 23 '18 at 2:40
Can you post your
onCreateOptionsMenu()
– Savin Sharma
Nov 23 '18 at 2:40
@SavinSharma I think he confused
onCreateOptionsMenu()
as the click listener for PopupMenu
– jake
Nov 23 '18 at 2:43
@SavinSharma I think he confused
onCreateOptionsMenu()
as the click listener for PopupMenu
– jake
Nov 23 '18 at 2:43
I figured out what I was doing wrong. Thank you for the input anyways!
– Ian
Nov 24 '18 at 0:43
I figured out what I was doing wrong. Thank you for the input anyways!
– Ian
Nov 24 '18 at 0:43
add a comment |
2 Answers
2
active
oldest
votes
UPDATE: I thought you want to listen for
Toolbar
option's click. Here is
how to listen for click events inPopupMenu
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// get id in item parameter and switch case
return true;
}
});
add a comment |
I Think u have to add another method as below. You haven't mentioned above.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popupmenu, menu);
return true;
}
You can add this code in Your MainActivity.java. Or For More refer to this link.
Android Menu
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439912%2fhandling-a-menu-click-event-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
UPDATE: I thought you want to listen for
Toolbar
option's click. Here is
how to listen for click events inPopupMenu
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// get id in item parameter and switch case
return true;
}
});
add a comment |
UPDATE: I thought you want to listen for
Toolbar
option's click. Here is
how to listen for click events inPopupMenu
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// get id in item parameter and switch case
return true;
}
});
add a comment |
UPDATE: I thought you want to listen for
Toolbar
option's click. Here is
how to listen for click events inPopupMenu
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// get id in item parameter and switch case
return true;
}
});
UPDATE: I thought you want to listen for
Toolbar
option's click. Here is
how to listen for click events inPopupMenu
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// get id in item parameter and switch case
return true;
}
});
edited Nov 23 '18 at 2:38
answered Nov 23 '18 at 2:26
jakejake
3719
3719
add a comment |
add a comment |
I Think u have to add another method as below. You haven't mentioned above.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popupmenu, menu);
return true;
}
You can add this code in Your MainActivity.java. Or For More refer to this link.
Android Menu
add a comment |
I Think u have to add another method as below. You haven't mentioned above.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popupmenu, menu);
return true;
}
You can add this code in Your MainActivity.java. Or For More refer to this link.
Android Menu
add a comment |
I Think u have to add another method as below. You haven't mentioned above.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popupmenu, menu);
return true;
}
You can add this code in Your MainActivity.java. Or For More refer to this link.
Android Menu
I Think u have to add another method as below. You haven't mentioned above.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popupmenu, menu);
return true;
}
You can add this code in Your MainActivity.java. Or For More refer to this link.
Android Menu
answered Nov 23 '18 at 4:48
Tapan Kumar PatroTapan Kumar Patro
2981312
2981312
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439912%2fhandling-a-menu-click-event-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you post your
onCreateOptionsMenu()
– Savin Sharma
Nov 23 '18 at 2:40
@SavinSharma I think he confused
onCreateOptionsMenu()
as the click listener forPopupMenu
– jake
Nov 23 '18 at 2:43
I figured out what I was doing wrong. Thank you for the input anyways!
– Ian
Nov 24 '18 at 0:43