Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Farouk Charkas
1,957 PointsHow to detect a Overflow Menu item?
I have a Overflow Menu with one item. My goal is for that item to refresh a webpage when the item is tapped. But unfortunately, I could not use a OnClickListener for it. Is there a special ClickListener. If so, how?
3 Answers
Andrew Sheragy
16,379 PointsMenus have their own function for when an item is selected
@Override public boolean onOptionsItemSelected(MenuItem item)
Andrew Sheragy
16,379 PointsYou use item.getItemId()
Most people use a switch statement on that value and each case is the id of your possible menu options like R.id.your_menu
Farouk Charkas
1,957 PointsSadly, I dont get it. Can you put it in code? Where do I get the menu id, or is it title?
Farouk Charkas
1,957 PointsSadly, I dont get it. Can you put it in code? Where do I get the menu id, or is it title?
Andrew Sheragy
16,379 Points@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { menu.clear(); inflater.inflate(R.menu.your_menu, menu); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.your_menu_item_id: someFunction(); break; default: break; } return false; }
Farouk Charkas
1,957 PointsThank you
Farouk Charkas
1,957 PointsFarouk Charkas
1,957 PointsThank you. Last question. How is the menu button addressed, id?