Create A Pop Up Menu In Sketchware





To show a pop up menu when a button is clicked follow given steps:-



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 popupid = new PopupMenu(MainActivity.this, button1);
//Main activity.this will be your activity name
Menu menu = popupid.getMenu();
menu.add("Item 1");
menu.add("Item 2");



This code Initialize pop-up menu with two option Item 1 and Item 2
2. After this add another​ add source directly block and put this code


popupid.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item){
switch (item.getTitle().toString()){
case "Item 1":


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


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


4. Add another add source directly block and put following code in it:


break;
case "Item 2":


5. Add blocks which will be executed when the option "Item 2" 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;
}
});
popupid.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 popupid = new PopupMenu(MainActivity.this, _view);
//Main activity.this will be your activity name
Menu menu = popupid.getMenu();
menu.add("Delete");
menu.add("Show");

Post a Comment

Previous Post Next Post