Android Context Menu example

Albert Assaad
2 min readSep 25, 2019

Here’s an example for a Context Menu on an EditText:

Let’s say you want to popup a context menu while the user long click on an EditText (or any view) here are the steps that you need to take:

First, you have to pass the view through the registerForContextMenu(View view) function:

Now you have to override the function onCreateContextMenu(
menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) by hitting ctrl+o and search for it, next we need to add the menuItems to the menu as below :

In this case, the add function takes these arguments (int groupId, int itemId, int order, CharSequence title) which explanation below from the SDK documentation:

the add function can take these arguments too (int groupId, int itemId, int order, @StringRes int titleRes) which titleRes is the id for a String resource example (R.string.app_name).

Next, you would like to take action on the menuItem when clicked. You need to override the function onContextItemSelected(item: MenuItem): Boolean:

In the above code, we are showing a toast with the item id that we give when we create it in the onCreateContextMenu function, for example, the first MenuItem test1 will Toast 1

Of Course, if you have more than a view that needs another menu Items you have to call the registerForContextMenuadd() on the view and modify the following code in the onCreateContextMenu:

That’s the end of this tutorial, more to go … Your feedback is appreciated in the comments, Thank you.



Albert Assaad

Multi-platform developer (Android,IOS,Dynamics Nav,,,Python) interested in everything technology.