Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Handling Long-Press/Long-Click Events

Author: Ian Darwin
Published? true
FormatLanguage: WikiFormat

Problem:

You want to listen for long press / long click events and react to them, without having manually to check for multiple events.

Solution:

If you just want a context menu on long-press, use the Activity's registerForContextMenu(View) method.

Otherwise, in Android 3.0 and later, you can use the View class' setLongClickable() and setOnLongClickListener() methods, and provide an OnLongClickListener.

Discussion:

If you just want a context menu on long-press, you can use the Activity's registerForContextMenu(View) method. The calling Activity must implement the View.OnCreateContextMenuListener interface, which has only one method:

void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo +menuInfo)

This method will be invoked when the context menu for this view is required. It is customary to use this method to inflate your menu by resource id into the provided ContextMenu, as in a regular menu (see 1891).

Custom handling of long press events was problematic before the Honeycomb release. The recipe Handling longpress in a map shows how to handle long press by collapsing multiple events into a single event. This is a bit dodgy, so in 3.0 explicit support was added; the View class now has setLongClickable(boolean) to enable/disable long click support, and the corresponding setOnLongClickListener(OnLongClickListener) methods. In this example we listen for long clicks on a View, and respond by popping up a PopupMenu, which will be modal, and will appear in front of the ListView.

        final View myList = findViewById(R.id.myView);
        ...
        
        myView.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View view) {
				PopupMenu p = new PopupMenu(Main.this, view);
				p.getMenuInflater().inflate(
					R.layout.main_popup_menu, p.getMenu());
				p.show();
				return true;
			}        	
        });

The popup menu will be dismissed when you click one if its items; the list of menu items comes from the XML file menu/popupmenu.xml; this just contains a series of item elements with the text for the menu items.

Note that calling setOnLongClickListener() has the side effect of calling setLongClickEnabled(true).

Note also that adding an onClickListener to a ListView (or other multi-item view) does work as you might expect; the list items simply get dispatched as per a normal click. Instead, you must use the setOnItemLongClickListener method which takes, unsurprisingly, an instance of OnItemLonClickListener(), whose only method will be invoked when you long-press on one item in the list.

Download:

The source code for this project can be downloaded from http://projects.darwinsys.com/ListViewDemos-src.zip.

Download:

The source code for this project is in the Android Cookbook repository, http://github.com/IanDarwin/Android-Cookbook-Examples/,in the subdirectory ListViewDemos.
No records found.