Create pop-up menu in Sketchware

In order to display a pop-up menu on button click or on ListView item click in your Sketchware project, use the codes provided below.

PopupMenu on button click

1. To display a PopupMenu when a Button is clicked, in onButtonClick event, use add source directly block, and put following code in it:
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
Menu menu = popup.getMenu();
menu.add("Delete");
menu.add("Show");

This code creates a PopupMenu for button1, with two options Delete and Show.

The same code can be used for creating PopupMenu for ImageView, TextView or or any other widget by using their id in place of button1.

2. After this add another​ add source directly block and put following code in it:
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item){
switch (item.getTitle().toString()){
case "Delete":

This code sets OnMenuItemClickListener for the PopupMenu. Since the menu has two items Delete, and Show, we create a switch which detects the Title of item clicked. Then we set the case when Delete is clicked.

3. Add blocks which will be executed when the option "Delete" in PopupMenu is clicked.

4. Add another add source directly block and put following code in it:
break;
case "Show":

5. Add blocks which will be executed when the option "Show" in PopupMenu is clicked.
In image above Toast Block is used.

6. Add another add source directly block and put following code in it:
break;}
return true;
}
});
popup.show();

PopupMenu on ListView item click

For displaying a PopupMenu on ListView item click, use the same code as above. Just replace the id of view 'button1' with _view. The code will look like this:
PopupMenu popup = new PopupMenu(MainActivity.this, _param2);
Menu menu = popup.getMenu();
menu.add("Delete");
menu.add("Show");


Comments

  1. After following the whole "PopupMenu on button click" tutorial, im faced with a problem. After pressing the delete button, i get both the "Delete clicked " and "Show clicked" toast message. Im i doing something wrong or maybe is this outdated?

    ReplyDelete
    Replies
    1. break;
      case "Show":

      Use this in between.
      I will correct the code soon.

      Delete
  2. How to add CheckBox to PopUp Menu?

    ReplyDelete
  3. How do I do when I click Delete or Show changes the screen or do what I want?

    ReplyDelete
  4. This only works for two options i need three

    ReplyDelete
  5. Why i can't found "add source directly"?

    ReplyDelete
  6. Hello dear how to get popup layout with on click button

    ReplyDelete
  7. I want the item inside the pop up menu to open another layout when its clicked, how to do that?

    ReplyDelete
    Replies
    1. Do you mean activity? Cus you ca Really "open" a layout. Maybe you mean, visibility?

      Delete
    2. Use Intent setScreen code or block in the case of the PopupMenu item.

      Delete
  8. Hi ! How to set theme to popup menu ? Thanks

    ReplyDelete

Post a Comment

Popular posts from this blog

A Flash Light App in Sketchware

Create Music Player app in Sketchware

Simple car racing android game in Sketchware

In Sketchware: How to create a calculator for any simple formula?

Firebase auth in Sketchware for Login Activity