Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 897335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:52:17+00:00 2026-05-15T14:52:17+00:00

I added a custom menu to the menu button using the following code: @Override

  • 0

I added a custom menu to the menu button using the following code:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        if (getDisplayedView() instanceof WorkspaceView) {
            ((WorkspaceView) getDisplayedView()).showEditMenu();
        }
        return true;
    }

and

public void showEditMenu() {
        new EditMenu(lexs, ((Project) projects.getSelectedItem()).getName(), ((ProjectList) projectsList.getSelectedItem()).getName()).show();
    }

The EditMenu is implemented the following way:

public class EditMenu {

    private final String DELETE_PROJECT = "Projekt löschen";
    private final String DELETE_LIST = "Liste löschen";
    private final String RENAME_PROJECT = "Projekt umbenennen";
    private final String RENAME_LIST = "Liste umbenennen";
    private final String CLOSE = "Menü schliessen";

    private Context context;
    private String projectName;
    private String listName;
    private AlertDialog alert;

    private final CharSequence[] items = {DELETE_PROJECT, DELETE_LIST, RENAME_PROJECT, RENAME_LIST, CLOSE};

    public EditMenu(Context context, String projectName, String listName) {
        this.context = context;
        this.projectName = projectName;
        this.listName = listName;
    }

    public void show() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(projectName + ": " + listName);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals(DELETE_PROJECT)) {
                    deleteProject();
                } else if (items[item].equals(DELETE_LIST)) {
                    deleteList();
                } else if (items[item].equals(RENAME_PROJECT)) {
                    renameProject();
                } else if (items[item].equals(RENAME_LIST)) {
                    renameList();
                } else if (items[item].equals(CLOSE)) {
                    close();
                }
            }
        });
        alert = builder.create();
        alert.show();
    }

    private void deleteProject() {

    }

    private void deleteList() {

    }

    private void renameProject() {

    }

    private void renameList() {

    }

    private void close() {

    }
}

This works correctly if I click the menu button the first time. But if the context menu is closed and i click the menu button a second time, nothing happens.

I also tried to call

alert.close(), alert.hide(), alert.dismiss(), etc in the method close(), but it doesn’t improve the situation. any hints? thankS¨!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:52:18+00:00Added an answer on May 15, 2026 at 2:52 pm

    Since there is no other answer in almost 3 weeks, I’ll answer my question by myself:

    Instead of overwriting

    public boolean onCreateOptionsMenu(Menu menu)
    

    one has to override

    public boolean onPrepareOptionsMenu(Menu menu)
    

    Here a short example how to do it:

    In the activity there is the following code:

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            showMenu();
            return true;
    }
    
    private void showMenu() {
            EditMenu menu = new EditMenu(this, "Pacman Menu");
            menu.show();
    }
    

    Then the clsas EditMenu looks for example the following way:

    public class EditMenu {
    
        private final String QUIT = "Quit";
        private final String RESTART = "New Game";
        private final String SOUND = "Switch Sound";
        private final String PAUSE = "Un/pause";
        private final CharSequence[] items = new CharSequence[] {QUIT, RESTART, SOUND, PAUSE};
    
        private Context context;
        private String title;
        private AlertDialog alert;
        private MenuListener listener = new MenuListener();
    
        public EditMenu(Context context, String title) {
            this.context = context;
            this.title = title;
        }
    
        public void show() {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setIcon(R.drawable.splashscreen);
            builder.setTitle(title);
            builder.setItems(items, listener);
            alert = builder.create();
            alert.show();
        }
    
        private class MenuListener implements DialogInterface.OnClickListener {
    
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals(QUIT)) {
                    ((PacmanGame) context).quitGame();
                } else if (items[item].equals(RESTART)) {
                    ((PacmanGame) context).restart();
                } else if (items[item].equals(SOUND)) {
                    Sound.setSoundOn(! Sound.isSoundOn());
                } else if (items[item].equals(PAUSE)) {
                    ((PacmanGame) context).getGameBoard().setPausing(!(((PacmanGame) context).getGameBoard().isPaused()));
                }
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 465k
  • Answers 465k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, I seem to be getting views but no responses.… May 16, 2026 at 1:21 am
  • Editorial Team
    Editorial Team added an answer #!usr/bin/perl use strict; use warnings; my $file_name = "file.txt"; open(my… May 16, 2026 at 1:21 am
  • Editorial Team
    Editorial Team added an answer Well, you will gain next to nothing with this setup;… May 16, 2026 at 1:21 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.