Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

Gallery

public class Gallery
extends AbsSpinner implements GestureDetector.OnGestureListener

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.AdapterView<android.widget.SpinnerAdapter>
         ↳ android.widget.AbsSpinner
           ↳ android.widget.Gallery


This class was deprecated in API level 16.
This widget is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library.

A view that shows items in a center-locked, horizontally scrolling list.

The default values for the Gallery assume you will be using R.styleable.Theme_galleryItemBackground as the background for each View given to the Gallery from the Adapter. If you are not doing this, you may need to adjust some Gallery properties, such as the spacing.

Views given to the Gallery should use Gallery.LayoutParams as their layout parameters type.

Summary

Nested classes

class Gallery.LayoutParams

Gallery extends LayoutParams to provide a place to hold current Transformation information along with previous position/transformation info. 

XML attributes

android:animationDuration Sets how long a transition animation should run (in milliseconds) when layout has changed. 
android:gravity Specifies how an object should position its content, on both the X and Y axes, within its own bounds. 
android:spacing  
android:unselectedAlpha Sets the alpha on the items that are not selected. 

Inherited XML attributes

Inherited constants

Inherited fields

Public constructors

Gallery(Context context)
Gallery(Context context, AttributeSet attrs)
Gallery(Context context, AttributeSet attrs, int defStyleAttr)
Gallery(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

boolean dispatchKeyEvent(KeyEvent event)

Dispatch a key event to the next view on the focus path.

void dispatchSetSelected(boolean selected)

Dispatch setSelected to all of this View's children.

ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)

Returns a new set of layout parameters based on the supplied attributes set.

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

boolean onDown(MotionEvent e)

Notified when a tap occurs with the down MotionEvent that triggered it.

boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)

Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent.

boolean onKeyDown(int keyCode, KeyEvent event)

Handles left, right, and clicking

boolean onKeyUp(int keyCode, KeyEvent event)

Default implementation of KeyEvent.Callback.onKeyUp(): perform clicking of the view when KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_ENTER or KeyEvent.KEYCODE_SPACE is released.

void onLongPress(MotionEvent e)

Notified when a long press occurs with the initial on down MotionEvent that trigged it.

boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)

Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent.

void onShowPress(MotionEvent e)

The user has performed a down MotionEvent and not performed a move or up yet.

boolean onSingleTapUp(MotionEvent e)

Notified when a tap occurs with the up MotionEvent that triggered it.

boolean onTouchEvent(MotionEvent event)

Implement this method to handle touch screen motion events.

void setAnimationDuration(int animationDurationMillis)

Sets how long the transition animation should run when a child view changes position.

void setCallbackDuringFling(boolean shouldCallback)

Whether or not to callback on any AdapterView.getOnItemSelectedListener() while the items are being flinged.

void setGravity(int gravity)

Describes how the child views are aligned.

void setSpacing(int spacing)

Sets the spacing between items in a Gallery

void setUnselectedAlpha(float unselectedAlpha)

Sets the alpha of items that are not selected in the Gallery.

boolean showContextMenu()

Shows the context menu for this view.

boolean showContextMenu(float x, float y)

Shows the context menu for this view anchored to the specified view-relative coordinate.

boolean showContextMenuForChild(View originalView)

Shows the context menu for the specified view or its ancestors.

boolean showContextMenuForChild(View originalView, float x, float y)

Shows the context menu for the specified view or its ancestors anchored to the specified view-relative coordinate.

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
int computeHorizontalScrollExtent()

Compute the horizontal extent of the horizontal scrollbar's thumb within the horizontal range.

int computeHorizontalScrollOffset()

Compute the horizontal offset of the horizontal scrollbar's thumb within the horizontal range.

int computeHorizontalScrollRange()

Compute the horizontal range that the horizontal scrollbar represents.

void dispatchSetPressed(boolean pressed)

Dispatch setPressed to all of this View's children.

ViewGroup.LayoutParams generateDefaultLayoutParams()

Returns a set of default layout parameters.

ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)

Returns a safe set of layout parameters based on the supplied layout params.

int getChildDrawingOrder(int childCount, int i)

Returns the index of the child to draw for this iteration.

boolean getChildStaticTransformation(View child, Transformation t)

Sets t to be the static transformation of the child, if set, returning a boolean to indicate whether a static transform was set.

ContextMenu.ContextMenuInfo getContextMenuInfo()

Views should implement this if they have extra information to associate with the context menu.

void onAttachedToWindow()

This is called when the view is attached to a window.

void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)

Called by the view system when the focus state of this view changes.

void onLayout(boolean changed, int l, int t, int r, int b)

Called from layout when this view should assign a size and position to each of its children.

Inherited methods

XML attributes

android:animationDuration

Sets how long a transition animation should run (in milliseconds) when layout has changed. Only relevant if animation is turned on.

May be an integer value, such as "100".

Related methods:

android:gravity

Specifies how an object should position its content, on both the X and Y axes, within its own bounds.

Must be one or more (separated by '|') of the following constant values.

ConstantValueDescription
bottom50Push object to the bottom of its container, not changing its size.
center11Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
center_horizontal1Place object in the horizontal center of its container, not changing its size.
center_vertical10Place object in the vertical center of its container, not changing its size.
clip_horizontal8Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.
clip_vertical80Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
end800005Push object to the end of its container, not changing its size.
fill77Grow the horizontal and vertical size of the object if needed so it completely fills its container.
fill_horizontal7Grow the horizontal size of the object if needed so it completely fills its container.
fill_vertical70Grow the vertical size of the object if needed so it completely fills its container.
left3Push object to the left of its container, not changing its size.
right5Push object to the right of its container, not changing its size.
start800003Push object to the beginning of its container, not changing its size.
top30Push object to the top of its container, not changing its size.

Related methods:

android:spacing

Related methods:

android:unselectedAlpha

Sets the alpha on the items that are not selected.

May be a floating point value, such as "1.2".

Related methods:

Public constructors

Gallery

added in API level 1
public Gallery (Context context)

Parameters
context Context

Gallery

added in API level 1
public Gallery (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

Gallery

added in API level 1
public Gallery (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Gallery

added in API level 21
public Gallery (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

dispatchKeyEvent

added in API level 1
public boolean dispatchKeyEvent (KeyEvent event)

Dispatch a key event to the next view on the focus path. This path runs from the top of the view tree down to the currently focused view. If this view has focus, it will dispatch to itself. Otherwise it will dispatch the next node down the focus path. This method also fires any key listeners.

Parameters
event KeyEvent: The key event to be dispatched.

Returns
boolean True if the event was handled, false otherwise.

dispatchSetSelected

added in API level 1
public void dispatchSetSelected (boolean selected)

Dispatch setSelected to all of this View's children.

Parameters
selected boolean: The new selected state

generateLayoutParams

added in API level 1
public ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)

Returns a new set of layout parameters based on the supplied attributes set.

Parameters
attrs AttributeSet: the attributes to build the layout parameters from

Returns
ViewGroup.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

getAccessibilityClassName

added in API level 23
public CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

onDown

added in API level 1
public boolean onDown (MotionEvent e)

Notified when a tap occurs with the down MotionEvent that triggered it. This will be triggered immediately for every down event. All other events should be preceded by this.

Parameters
e MotionEvent: The down motion event.

Returns
boolean

onFling

added in API level 1
public boolean onFling (MotionEvent e1, 
                MotionEvent e2, 
                float velocityX, 
                float velocityY)

Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent. The calculated velocity is supplied along the x and y axis in pixels per second.

Parameters
e1 MotionEvent: The first down motion event that started the fling.

e2 MotionEvent: The move motion event that triggered the current onFling.

velocityX float: The velocity of this fling measured in pixels per second along the x axis.

velocityY float: The velocity of this fling measured in pixels per second along the y axis.

Returns
boolean true if the event is consumed, else false

onKeyDown

added in API level 1
public boolean onKeyDown (int keyCode, 
                KeyEvent event)

Handles left, right, and clicking

Parameters
keyCode int: a key code that represents the button pressed, from KeyEvent

event KeyEvent: the KeyEvent object that defines the button action

Returns
boolean If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

onKeyUp

added in API level 1
public boolean onKeyUp (int keyCode, 
                KeyEvent event)

Default implementation of KeyEvent.Callback.onKeyUp(): perform clicking of the view when KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_ENTER or KeyEvent.KEYCODE_SPACE is released.

Key presses in software keyboards will generally NOT trigger this listener, although some may elect to do so in some situations. Do not rely on this to catch software key presses.

Parameters
keyCode int: A key code that represents the button pressed, from KeyEvent.

event KeyEvent: The KeyEvent object that defines the button action.

Returns
boolean If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

onLongPress

added in API level 1
public void onLongPress (MotionEvent e)

Notified when a long press occurs with the initial on down MotionEvent that trigged it.

Parameters
e MotionEvent: The initial on down motion event that started the longpress.

This value must never be null.

onScroll

added in API level 1
public boolean onScroll (MotionEvent e1, 
                MotionEvent e2, 
                float distanceX, 
                float distanceY)

Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent. The distance in x and y is also supplied for convenience.

Parameters
e1 MotionEvent: The first down motion event that started the scrolling.

e2 MotionEvent: The move motion event that triggered the current onScroll.

distanceX float: The distance along the X axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.

distanceY float: The distance along the Y axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.

Returns
boolean true if the event is consumed, else false

onShowPress

added in API level 1
public void onShowPress (MotionEvent e)

The user has performed a down MotionEvent and not performed a move or up yet. This event is commonly used to provide visual feedback to the user to let them know that their action has been recognized i.e. highlight an element.

Parameters
e MotionEvent: The down motion event

onSingleTapUp

added in API level 1
public boolean onSingleTapUp (MotionEvent e)

Notified when a tap occurs with the up MotionEvent that triggered it.

Parameters
e MotionEvent: The up motion event that completed the first tap

Returns
boolean true if the event is consumed, else false

onTouchEvent

added in API level 1
public boolean onTouchEvent (MotionEvent event)

Implement this method to handle touch screen motion events.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
event MotionEvent: The motion event.

Returns
boolean True if the event was handled, false otherwise.

setAnimationDuration

added in API level 1
public void setAnimationDuration (int animationDurationMillis)

Sets how long the transition animation should run when a child view changes position. Only relevant if animation is turned on.

Related XML Attributes:

Parameters
animationDurationMillis int: The duration of the transition, in milliseconds.

setCallbackDuringFling

added in API level 1
public void setCallbackDuringFling (boolean shouldCallback)

Whether or not to callback on any AdapterView.getOnItemSelectedListener() while the items are being flinged. If false, only the final selected item will cause the callback. If true, all items between the first and the final will cause callbacks.

Parameters
shouldCallback boolean: Whether or not to callback on the listener while the items are being flinged.

setGravity

added in API level 1
public void setGravity (int gravity)

Describes how the child views are aligned.

Related XML Attributes:

setSpacing

added in API level 1
public void setSpacing (int spacing)

Sets the spacing between items in a Gallery

Related XML Attributes:

Parameters
spacing int: The spacing in pixels between items in the Gallery

setUnselectedAlpha

added in API level 1
public void setUnselectedAlpha (float unselectedAlpha)

Sets the alpha of items that are not selected in the Gallery.

Related XML Attributes:

Parameters
unselectedAlpha float: the alpha for the items that are not selected.

showContextMenu

added in API level 1
public boolean showContextMenu ()

Shows the context menu for this view.

Returns
boolean true if the context menu was shown, false otherwise

showContextMenu

added in API level 24
public boolean showContextMenu (float x, 
                float y)

Shows the context menu for this view anchored to the specified view-relative coordinate.

Parameters
x float: the X coordinate in pixels relative to the view to which the menu should be anchored, or Float.NaN to disable anchoring

y float: the Y coordinate in pixels relative to the view to which the menu should be anchored, or Float.NaN to disable anchoring

Returns
boolean true if the context menu was shown, false otherwise

showContextMenuForChild

added in API level 1
public boolean showContextMenuForChild (View originalView)

Shows the context menu for the specified view or its ancestors.

In most cases, a subclass does not need to override this. However, if the subclass is added directly to the window manager (for example, ViewManager.addView(View, android.view.ViewGroup.LayoutParams)) then it should override this and show the context menu.

Parameters
originalView View: the source view where the context menu was first invoked

Returns
boolean true if the context menu was shown, false otherwise

showContextMenuForChild

added in API level 24
public boolean showContextMenuForChild (View originalView, 
                float x, 
                float y)

Shows the context menu for the specified view or its ancestors anchored to the specified view-relative coordinate.

In most cases, a subclass does not need to override this. However, if the subclass is added directly to the window manager (for example, ViewManager.addView(View, android.view.ViewGroup.LayoutParams)) then it should override this and show the context menu.

If a subclass overrides this method it should also override showContextMenuForChild(View).

Parameters
originalView View: the source view where the context menu was first invoked

x float: the X coordinate in pixels relative to the original view to which the menu should be anchored, or Float.NaN to disable anchoring

y float: the Y coordinate in pixels relative to the original view to which the menu should be anchored, or Float.NaN to disable anchoring

Returns
boolean true if the context menu was shown, false otherwise

Protected methods

checkLayoutParams

added in API level 1
protected boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

computeHorizontalScrollExtent

added in API level 1
protected int computeHorizontalScrollExtent ()

Compute the horizontal extent of the horizontal scrollbar's thumb within the horizontal range. This value is used to compute the length of the thumb within the scrollbar's track.

The range is expressed in arbitrary units that must be the same as the units used by computeHorizontalScrollRange() and computeHorizontalScrollOffset().

The default extent is the drawing width of this view.

Returns
int the horizontal extent of the scrollbar's thumb

computeHorizontalScrollOffset

added in API level 1
protected int computeHorizontalScrollOffset ()

Compute the horizontal offset of the horizontal scrollbar's thumb within the horizontal range. This value is used to compute the position of the thumb within the scrollbar's track.

The range is expressed in arbitrary units that must be the same as the units used by computeHorizontalScrollRange() and computeHorizontalScrollExtent().

The default offset is the scroll offset of this view.

Returns
int the horizontal offset of the scrollbar's thumb

computeHorizontalScrollRange

added in API level 1
protected int computeHorizontalScrollRange ()

Compute the horizontal range that the horizontal scrollbar represents.

The range is expressed in arbitrary units that must be the same as the units used by computeHorizontalScrollExtent() and computeHorizontalScrollOffset().

The default range is the drawing width of this view.

Returns
int the total horizontal range represented by the horizontal scrollbar

dispatchSetPressed

added in API level 1
protected void dispatchSetPressed (boolean pressed)

Dispatch setPressed to all of this View's children.

Parameters
pressed boolean: The new pressed state

generateDefaultLayoutParams

added in API level 1
protected ViewGroup.LayoutParams generateDefaultLayoutParams ()

Returns a set of default layout parameters. These parameters are requested when the View passed to addView(View) has no layout parameters already set. If null is returned, an exception is thrown from addView.

Returns
ViewGroup.LayoutParams a set of default layout parameters or null

generateLayoutParams

added in API level 1
protected ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

Returns a safe set of layout parameters based on the supplied layout params. When a ViewGroup is passed a View whose layout params do not pass the test of checkLayoutParams(android.view.ViewGroup.LayoutParams), this method is invoked. This method should return a new set of layout params suitable for this ViewGroup, possibly by copying the appropriate attributes from the specified set of layout params.

Parameters
p ViewGroup.LayoutParams: The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.

Returns
ViewGroup.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

getChildDrawingOrder

added in API level 1
protected int getChildDrawingOrder (int childCount, 
                int i)

Returns the index of the child to draw for this iteration. Override this if you want to change the drawing order of children. By default, it returns i.

NOTE: In order for this method to be called, you must enable child ordering first by calling setChildrenDrawingOrderEnabled(boolean).

Parameters
childCount int

i int: The current iteration.

Returns
int The index of the child to draw this iteration.

getChildStaticTransformation

added in API level 1
protected boolean getChildStaticTransformation (View child, 
                Transformation t)

Sets t to be the static transformation of the child, if set, returning a boolean to indicate whether a static transform was set. The default implementation simply returns false; subclasses may override this method for different behavior. setStaticTransformationsEnabled(boolean) must be set to true for this method to be called.

Parameters
child View: The child view whose static transform is being requested

t Transformation: The Transformation which will hold the result

Returns
boolean true if the transformation was set, false otherwise

getContextMenuInfo

added in API level 1
protected ContextMenu.ContextMenuInfo getContextMenuInfo ()

Views should implement this if they have extra information to associate with the context menu. The return result is supplied as a parameter to the View.OnCreateContextMenuListener.onCreateContextMenu(ContextMenu, View, ContextMenuInfo) callback.

Returns
ContextMenu.ContextMenuInfo Extra information about the item for which the context menu should be shown. This information will vary across different subclasses of View.

onAttachedToWindow

added in API level 1
protected void onAttachedToWindow ()

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before onDraw(android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after onMeasure(int, int).

onFocusChanged

added in API level 1
protected void onFocusChanged (boolean gainFocus, 
                int direction, 
                Rect previouslyFocusedRect)

Called by the view system when the focus state of this view changes. When the focus change event is caused by directional navigation, direction and previouslyFocusedRect provide insight into where the focus is coming from. When overriding, be sure to call up through to the super class so that the standard focus handling will occur.

Parameters
gainFocus boolean: True if the View has focus; false otherwise.

direction int: The direction focus has moved when requestFocus() is called to give this view focus. Values are View.FOCUS_UP, View.FOCUS_DOWN, View.FOCUS_LEFT, View.FOCUS_RIGHT, View.FOCUS_FORWARD, or View.FOCUS_BACKWARD. It may not always apply, in which case use the default.

previouslyFocusedRect Rect: The rectangle, in this view's coordinate system, of the previously focused view. If applicable, this will be passed in as finer grained information about where the focus is coming from (in addition to direction). Will be null otherwise.

onLayout

added in API level 1
protected void onLayout (boolean changed, 
                int l, 
                int t, 
                int r, 
                int b)

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed boolean: This is a new size or position for this view

l int: Left position, relative to parent

t int: Top position, relative to parent

r int: Right position, relative to parent

b int: Bottom position, relative to parent