Enter your keyword

Android ListView Context Menu

Android ListView Context Menu

Android ListView Context Menu

Very often you will wonder how other Android applications are able to show a Context Menu when you long press on a ListView item. In this tutorial am going to demonstrate exactly how to do so.

Let’s start by creating the ListView.

 

Now let’s, create an array of countries resource that will be populated onto the ListView.

Create an ArrayAdapter to load the countries array onto the ListView.

If you run your application at this point, you should have output similar to the one below.

0. Countries_2

Now lets, create the Context Menu array resource.

Lets register our ListView for a Context Menu.

Now implement the onCreateContextMenu(). This method determine what to do once the Context Menu is created.

Implement the onContextItemSelected() to add specific actions to the ContextMenu items we created.

That’s all we had to do to achieve the result below

1. Country - context menu_2

 

Download Project