Android Core

Resolving the dual ItemClick conundrum for the Android Gallery View

Yes, the Gallery View is deprecated, however currently there really isn’t an out of the box solution to what the gallery view provides, which is a center locked horizontal scrolling list of items. The Twoway-view project has some potential in addressing this need, but it needs time to mature a bit.

There are many issues with the Gallery view, but one of the most significant is if you need the ability to show context menus for an item. If you are using it with a D-Pad (say in an Android TV application), and you long press on an item using D-PAD OK, or BUTTON-A, you’ll get both ItemLongClick and ItemClick events fired by the gallery. This has been a long standing bug within the app, an has plagued me for years. I finally tracked it down and created the following gist that can be applied to a custom version of the Gallery.

  * This change should work in both the original Gallery code as well as in the vairous open source
  * extensions and variations.
  * Find the displatchLongPress method in the Gallery class file. and update it to the following.
  * Make sure that longPressHandled is a field available to all the other methods.
    boolean longPressHandled =  false;
	private boolean dispatchLongPress(View view, int position, long id) {
		longPressHandled = false;

		if (mOnItemLongClickListener != null) {
			longPressHandled = mOnItemLongClickListener.onItemLongClick(this,
					mDownTouchView, mDownTouchPosition, id);

		if (!longPressHandled) {
			mContextMenuInfo = new AdapterContextMenuInfo(view, position, id);
			longPressHandled = super.showContextMenuForChild(this);

		if (longPressHandled) {

		return longPressHandled;

     * Find the onKeyUp method and change it to the following.
     * Since dispatchLongPress is fired before the onKeyUp event, we have
     * to check to see if the onItemLongClickListener handled the event.
     * If so do not performItemClick, and always reset the longPressHandled to false.
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_ENTER: {

			if (mReceivedInvokeKeyDown) {
				if (mItemCount > 0) {

					postDelayed(new Runnable() {
						public void run() {
					}, ViewConfiguration.getPressedStateDuration());

					int selectedIndex = mSelectedPosition - mFirstPosition;

					if (!longPressHandled) {

			// Clear the flag
			mReceivedInvokeKeyDown = false;
			longPressHandled = false;

			return true;

		return super.onKeyUp(keyCode, event);

Basically, what was happening is that the onKeyUp event that fired the ItemClick event never checked to see if the dispatchLongPress event had already handled the event. So it was always firing the ItemClick event. The gist makes this small change, and actually eliminates numerous work arounds that had to be done to get context menus to show with the gallery.

This bug only affects you if you needed to use a D-PAD, Remote Control, or Game Controller as an input device. Normal touch screen events are handled correctly.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button