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) {
			performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
		}

		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.
     * 
     */
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_CENTER:
		case KeyEvent.KEYCODE_ENTER: {

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

					dispatchPress(mSelectedChild);
					postDelayed(new Runnable() {
						@Override
						public void run() {
							dispatchUnpress();
						}
					}, ViewConfiguration.getPressedStateDuration());

					int selectedIndex = mSelectedPosition - mFirstPosition;

					if (!longPressHandled) {
						performItemClick(getChildAt(selectedIndex),
								mSelectedPosition,
								mAdapter.getItemId(mSelectedPosition));
					}
				}
			}

			// 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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


2 − one =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close