From Juneday education
Jump to: navigation, search


This chapter will introduce you to a structured way for presenting many items in a scrollable list.


Note: There's a missing method call in this video. You need to add the following to your Activity: registerForContextMenu(listView); in order to tell Android that you want to use ContextMenu.

All videos in this channel: ListView


In short you only need to do the following to use a ListView with a ContextMenu. The complete source to steps below can be found at github: listviewexample.

Note: There are other ways to create na use a ListView.

Create a ListView

Add the following to your Activity's layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""



Create Adapater

You need to create an Adapter and use that adapter in the ListView.In your Activity, typically in the onCreate:

        // Lookup ListView
        ListView listView = (ListView) findViewById(;

        // Create Adapter
        adapter = new ArrayAdapter<Member>(this,

        // Set listView's adapter to the new adapter

Handle ListView clicks

When a user clicks on an item the following method is invoked: onItemClick. Add an implementation of that method to your Activity:

        listView.setOnItemClickListener(new ListView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent,
                                    final View view,
                                    int position /*The position of the view in the adapter.*/,
                                    long id /* The row id of the item that was clicked */) {
                Log.d(LOG_TAG, "item clicked, pos:" + position + " id: " + id);

Register for ContextMenu

In your Activity, typically in the onCreate:


Create ContextMenu

When a user clicks (long click) on an item the following method is invoked: onCreateContextMenu. Add an implementation of that method to your Activity:

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        ListView listView = (ListView) v;
        AdapterView.AdapterContextMenuInfo acMenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
        Member member = (Member) listView.getItemAtPosition(acMenuInfo.position);
        menu.setHeaderTitle("Manager " + member);
        menu.add(Menu.NONE, MENU_ENTRY_DELETE, Menu.NONE, "Delete");
        menu.add(Menu.NONE, MENU_ENTRY_DUPLICATE, Menu.NONE, "Duplicate");

Handle ContextMenu click

When a user clicks on a menu item (in the ContextMenu) the following method is invoked: onContextItemSelected. Add an implementation of that method to your Activity:

    public boolean onContextItemSelected(MenuItem item) {
        // get extra information set by the View that added this menu item
        AdapterView.AdapterContextMenuInfo info =
                (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        Log.d(LOG_TAG, " itemId: " + item.getItemId());
        Log.d(LOG_TAG, " title:  " + item.getTitle());
        if (item.getItemId() == MENU_ENTRY_DUPLICATE) {
            Log.d(LOG_TAG, " member: " + members.get(info.position));
        return true;

External videos


Exercises for this chapter

There are no exercises in this chapter. Instead we have a couple of exercises on ListView in the coming chapter Android:Exercsises_Your_own_classes.


  • No files for this chapter

External links (further reading)

Books this chapter is a part of

Android - the practical way

Book TOC | previous chapter | next chapter