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");
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");
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?
ReplyDeletebreak;
Deletecase "Show":
Use this in between.
I will correct the code soon.
How to add CheckBox to PopUp Menu?
ReplyDeleteHow do I do when I click Delete or Show changes the screen or do what I want?
ReplyDeleteThis only works for two options i need three
ReplyDeleteWhy i can't found "add source directly"?
ReplyDeleteHello dear how to get popup layout with on click button
ReplyDeleteI want the item inside the pop up menu to open another layout when its clicked, how to do that?
ReplyDeleteDo you mean activity? Cus you ca Really "open" a layout. Maybe you mean, visibility?
DeleteUse Intent setScreen code or block in the case of the PopupMenu item.
DeleteHow to customize popup menu?
ReplyDeleteHow to customize popup menu?
ReplyDeleteSS ML
ReplyDeleteHi ! How to set theme to popup menu ? Thanks
ReplyDeleteThanks
ReplyDeleteHow i change popup menu text color
ReplyDelete