Is it possible to create a dynamic context menu in android?
What I mean is, for example, if I have a list of items, that can be in two states: “read” and “unread”. I want to be able to bring up a context menu with an option of “Mark as Read” for the unread items, and “Mark as Unread” for the read items.
So clicking on:
> read
> unread <-- click
> read
will show context menu “Mark as Read” resulting in:
> read
> read
> read <-- click
will show the menu “Mark as Unread”.
Is there some function that allows me to customize the creation of the context menu, just before it is displayed?
Any help is welcome!
As you don’t provide code, this is is the basic approach:
In this case, I’m assuming the list is populated with
TextViews that can have the string “read” or “unread” in it. As I already said, this is a very basic approach. The important thing here is to notice how theContextMenu.ContextMenuInfoobject is being used.Also, to read the state of the item that was selected you can use the
item.getMenuInfo()method inside theonContextItemSelected(MenuItem item)method.