I'm trying to create a simple menu with one button that will call a method to clear the array. I don't want to use xml because all I need is one button.
Something like this -
public boolean onCreateOptionsMenu(Menu menu) {
button "Clear Array";
onClick{// run method that wipes array};
return true;
}
Thank you
Something like this might work:
Menu
gives us a handy method,add()
, which allows you to add a MenuItem. So we make one. Then we assign anOnMenuItemClickListener
to theMenuItem
and override itsonMenuItemClick()
to do what we want it to do.Here i implemented popup menu dynamically by using a click listener.
Programmatically, I was able to create a simple menu using the following code.
A--C's method works, but you should avoid setting the click listeners manually. Especially when you have multiple menu items.
I prefer this way:
By using this approach you avoid creating unecessary objects (listeners) and I also find this code easier to read and understand.