Added in API level 1
Deprecated in API level 16

Gallery


open class Gallery : AbsSpinner, GestureDetector.OnGestureListener
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ android.widget.AdapterView<android.widget.SpinnerAdapter>
   ↳ android.widget.AbsSpinner
   ↳ android.widget.Gallery

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

The default values for the Gallery assume you will be using android.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
open

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
Inherited XML attributes
Inherited constants
Public constructors
Gallery(context: Context!)

Gallery(context: Context!, attrs: AttributeSet!)

Gallery(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

Gallery(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods
open Boolean

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

open Unit

Dispatch setSelected to all of this View's children.

open ViewGroup.LayoutParams!

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

open CharSequence!

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

open Boolean

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

open Boolean
onFling(e1: MotionEvent?, e2: MotionEvent, velocityX: Float, velocityY: Float)

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

open Boolean
onKeyDown(keyCode: Int, event: KeyEvent!)

Handles left, right, and clicking

open Boolean
onKeyUp(keyCode: Int, event: KeyEvent!)

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.

open Unit

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

open Boolean
onScroll(e1: MotionEvent?, e2: MotionEvent, distanceX: Float, distanceY: Float)

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

open Unit

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

open Boolean

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

open Boolean

Implement this method to handle pointer events.

open Unit
setAnimationDuration(animationDurationMillis: Int)

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

open Unit

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

open Unit
setGravity(gravity: Int)

Describes how the child views are aligned.

open Unit
setSpacing(spacing: Int)

Sets the spacing between items in a Gallery

open Unit
setUnselectedAlpha(unselectedAlpha: Float)

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

open Boolean

Shows the context menu for this view.

open Boolean

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

open Boolean
showContextMenuForChild(originalView: View!)

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

open Boolean
showContextMenuForChild(originalView: View!, x: Float, y: Float)

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

Protected methods
open Boolean

open Int

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

open Int

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

open Int

Compute the horizontal range that the horizontal scrollbar represents.

open Unit

Dispatch setPressed to all of this View's children.

open ViewGroup.LayoutParams!

Returns a set of default layout parameters.

open ViewGroup.LayoutParams!

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

open Int
getChildDrawingOrder(childCount: Int, i: Int)

Converts drawing order position to container position.

open Boolean

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

open ContextMenu.ContextMenuInfo!

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

open Unit

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

open Unit
onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?)

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

open Unit
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

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

Inherited functions
Inherited properties

XML attributes

android:animationDuration

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

android:gravity

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.

Constant Value Description
bottom 50 Push object to the bottom of its container, not changing its size.
center 11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
center_horizontal 1 Place object in the horizontal center of its container, not changing its size.
center_vertical 10 Place object in the vertical center of its container, not changing its size.
clip_horizontal 8 Additional 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_vertical 80 Additional 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.
end 800005 Push object to the end of its container, not changing its size.
fill 77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.
fill_horizontal 7 Grow the horizontal size of the object if needed so it completely fills its container.
fill_vertical 70 Grow the vertical size of the object if needed so it completely fills its container.
left 3 Push object to the left of its container, not changing its size.
right 5 Push object to the right of its container, not changing its size.
start 800003 Push object to the beginning of its container, not changing its size.
top 30 Push object to the top of its container, not changing its size.

android:spacing

android:spacing

Public constructors

Added in API level 1
Gallery(context: Context!)
Added in API level 1
Gallery(
    context: Context!,
    attrs: AttributeSet!)
Added in API level 1
Gallery(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)
Added in API level 21
Gallery(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

Public methods

dispatchKeyEvent

Added in API level 1
open fun dispatchKeyEvent(event: KeyEvent!): Boolean

Deprecated: Deprecated in Java.

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.
Return
Boolean True if the event was handled, false otherwise.

dispatchSetSelected

Added in API level 1
open fun dispatchSetSelected(selected: Boolean): Unit

Deprecated: Deprecated in Java.

Dispatch setSelected to all of this View's children.

Parameters
selected Boolean: The new selected state

generateLayoutParams

Added in API level 1
open fun generateLayoutParams(attrs: AttributeSet!): ViewGroup.LayoutParams!

Deprecated: Deprecated in Java.

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

Parameters
attrs AttributeSet!: the attributes to build the layout parameters from
Return
ViewGroup.LayoutParams! an instance of android.view.ViewGroup.LayoutParams or one of its descendants

getAccessibilityClassName

Added in API level 23
Deprecated in API level 16
open fun getAccessibilityClassName(): CharSequence!

Deprecated: Deprecated in Java.

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.

onDown

Added in API level 1
open fun onDown(e: MotionEvent): Boolean

Deprecated: Deprecated in Java.

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.
This value cannot be null.

onFling

Added in API level 1
open fun onFling(
    e1: MotionEvent?,
    e2: MotionEvent,
    velocityX: Float,
    velocityY: Float
): Boolean

Deprecated: Deprecated in Java.

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. A null event indicates an incomplete event stream or error state.
e2 MotionEvent: The move motion event that triggered the current onFling.
This value cannot be null.
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.
Return
Boolean true if the event is consumed, else false

onKeyDown

Added in API level 1
open fun onKeyDown(
    keyCode: Int,
    event: KeyEvent!
): Boolean

Deprecated: Deprecated in Java.

Handles left, right, and clicking

Parameters
keyCode Int: a key code that represents the button pressed, from android.view.KeyEvent
event KeyEvent!: the KeyEvent object that defines the button action
Return
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
open fun onKeyUp(
    keyCode: Int,
    event: KeyEvent!
): Boolean

Deprecated: Deprecated in Java.

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 android.view.KeyEvent.
event KeyEvent!: The KeyEvent object that defines the button action.
Return
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
open fun onLongPress(e: MotionEvent): Unit

Deprecated: Deprecated in Java.

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

Parameters
e MotionEvent: This value cannot be null.

onScroll

Added in API level 1
open fun onScroll(
    e1: MotionEvent?,
    e2: MotionEvent,
    distanceX: Float,
    distanceY: Float
): Boolean

Deprecated: Deprecated in Java.

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. A null event indicates an incomplete event stream or error state.
e2 MotionEvent: The move motion event that triggered the current onScroll.
This value cannot be null.
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.
Return
Boolean true if the event is consumed, else false

onShowPress

Added in API level 1
open fun onShowPress(e: MotionEvent): Unit

Deprecated: Deprecated in Java.

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.
This value cannot be null.

onSingleTapUp

Added in API level 1
open fun onSingleTapUp(e: MotionEvent): Boolean

Deprecated: Deprecated in Java.

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

Parameters
e MotionEvent: The up motion event that completed the first tap.
This value cannot be null.
Return
Boolean true if the event is consumed, else false

onTouchEvent

Added in API level 1
open fun onTouchEvent(event: MotionEvent!): Boolean

Deprecated: Deprecated in Java.

Implement this method to handle pointer events.

This method is called to handle motion events where pointers are down on the view. For example, this could include touchscreen touches, stylus touches, or click-and-drag events from a mouse. However, it is not called for motion events that do not involve pointers being down, such as hover events or mouse scroll wheel movements.

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.
Return
Boolean True if the event was handled, false otherwise.

setAnimationDuration

Added in API level 1
open fun setAnimationDuration(animationDurationMillis: Int): Unit

Deprecated: Deprecated in Java.

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

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

setCallbackDuringFling

Added in API level 1
open fun setCallbackDuringFling(shouldCallback: Boolean): Unit

Deprecated: Deprecated in Java.

Whether or not to callback on any 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
open fun setGravity(gravity: Int): Unit

Deprecated: Deprecated in Java.

Describes how the child views are aligned.

Parameters
gravity Int:

setSpacing

Added in API level 1
open fun setSpacing(spacing: Int): Unit

Deprecated: Deprecated in Java.

Sets the spacing between items in a Gallery

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

setUnselectedAlpha

Added in API level 1
open fun setUnselectedAlpha(unselectedAlpha: Float): Unit

Deprecated: Deprecated in Java.

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

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

showContextMenu

Added in API level 1
open fun showContextMenu(): Boolean

Deprecated: Deprecated in Java.

Shows the context menu for this view.

Return
Boolean true if the context menu was shown, false otherwise

showContextMenu

Added in API level 24
Deprecated in API level 16
open fun showContextMenu(
    x: Float,
    y: Float
): Boolean

Deprecated: Deprecated in Java.

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
Return
Boolean true if the context menu was shown, false otherwise

showContextMenuForChild

Added in API level 1
open fun showContextMenuForChild(originalView: View!): Boolean

Deprecated: Deprecated in Java.

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
Return
Boolean true if the context menu was shown, false otherwise

showContextMenuForChild

Added in API level 24
Deprecated in API level 16
open fun showContextMenuForChild(
    originalView: View!,
    x: Float,
    y: Float
): Boolean

Deprecated: Deprecated in Java.

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(android.view.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
Return
Boolean true if the context menu was shown, false otherwise

Protected methods

checkLayoutParams

Added in API level 1
protected open fun checkLayoutParams(p: ViewGroup.LayoutParams!): Boolean

Deprecated: Deprecated in Java.

computeHorizontalScrollExtent

Added in API level 1
protected open fun computeHorizontalScrollExtent(): Int

Deprecated: Deprecated in Java.

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.

Return
Int the horizontal extent of the scrollbar's thumb

computeHorizontalScrollOffset

Added in API level 1
protected open fun computeHorizontalScrollOffset(): Int

Deprecated: Deprecated in Java.

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.

Return
Int the horizontal offset of the scrollbar's thumb

computeHorizontalScrollRange

Added in API level 1
protected open fun computeHorizontalScrollRange(): Int

Deprecated: Deprecated in Java.

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.

Return
Int the total horizontal range represented by the horizontal scrollbar

dispatchSetPressed

Added in API level 1
protected open fun dispatchSetPressed(pressed: Boolean): Unit

Deprecated: Deprecated in Java.

Dispatch setPressed to all of this View's children.

Parameters
pressed Boolean: The new pressed state

generateDefaultLayoutParams

Added in API level 1
protected open fun generateDefaultLayoutParams(): ViewGroup.LayoutParams!

Deprecated: Deprecated in Java.

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

Return
ViewGroup.LayoutParams! a set of default layout parameters or null

generateLayoutParams

Added in API level 1
protected open fun generateLayoutParams(p: ViewGroup.LayoutParams!): ViewGroup.LayoutParams!

Deprecated: Deprecated in Java.

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.
Return
ViewGroup.LayoutParams! an instance of android.view.ViewGroup.LayoutParams or one of its descendants

getChildDrawingOrder

Added in API level 1
protected open fun getChildDrawingOrder(
    childCount: Int,
    i: Int
): Int

Deprecated: Deprecated in Java.

Converts drawing order position to container position. Override this if you want to change the drawing order of children. By default, it returns drawingPosition.

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

Parameters
drawingPosition the drawing order position.
Return
Int the container position of a child for this drawing order position.

getChildStaticTransformation

Added in API level 1
protected open fun getChildStaticTransformation(
    child: View!,
    t: Transformation!
): Boolean

Deprecated: Deprecated in Java.

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
Return
Boolean true if the transformation was set, false otherwise

getContextMenuInfo

Added in API level 1
protected open fun getContextMenuInfo(): ContextMenu.ContextMenuInfo!

Deprecated: Deprecated in Java.

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 OnCreateContextMenuListener.onCreateContextMenu(ContextMenu, View, ContextMenuInfo) callback.

Return
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 open fun onAttachedToWindow(): Unit

Deprecated: Deprecated in Java.

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).
If you override this method you must call through to the superclass implementation.

onFocusChanged

Added in API level 1
protected open fun onFocusChanged(
    gainFocus: Boolean,
    direction: Int,
    previouslyFocusedRect: Rect?
): Unit

Deprecated: Deprecated in Java.

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.
If you override this method you must call through to the superclass implementation.

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 FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD, or FOCUS_BACKWARD. It may not always apply, in which case use the default.
Value is one of the following:
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 open fun onLayout(
    changed: Boolean,
    l: Int,
    t: Int,
    r: Int,
    b: Int
): Unit

Deprecated: Deprecated in Java.

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
left Left position, relative to parent
top Top position, relative to parent
right Right position, relative to parent
bottom Bottom position, relative to parent