Added in API level 1

GridView


open class GridView : AbsListView
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ android.widget.AdapterView<android.widget.ListAdapter>
   ↳ android.widget.AbsListView
   ↳ android.widget.GridView

A view that shows items in two-dimensional scrolling grid. The items in the grid come from the ListAdapter associated with this view.

See the Grid View guide.

Summary

XML attributes
android:columnWidth Specifies the fixed width for each column.
android:gravity Specifies the gravity within each cell.
android:horizontalSpacing Defines the default horizontal spacing between columns.
android:numColumns Defines how many columns to show.
android:stretchMode Defines how columns should stretch to fill the available empty space, if any.
android:verticalSpacing Defines the default vertical spacing between rows.
Inherited XML attributes
Constants
static Int

Creates as many columns as can fit on screen.

static Int

Disables stretching.

static Int

Stretches columns.

static Int

Stretches the spacing between columns.

static Int

Stretches the spacing between columns.

Inherited constants
Public constructors
GridView(context: Context!)

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

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

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

Public methods
open CharSequence!

open ListAdapter!

open Int

Return the width of a column in the grid.

open Int

Describes how the child views are horizontally aligned.

open Int

Returns the amount of horizontal spacing currently used between each item in the grid.

open Int

Get the number of columns in the grid.

open Int

Return the requested width of a column in the grid.

open Int

Returns the requested amount of horizontal spacing between each item in the grid.

open Int

open Int

Returns the amount of vertical spacing between each item in the grid.

open Unit

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

open Boolean
onKeyMultiple(keyCode: Int, repeatCount: Int, event: KeyEvent!)

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

open Unit

Sets the data behind this GridView.

open Unit
setColumnWidth(columnWidth: Int)

Set the width of columns in the grid.

open Unit
setGravity(gravity: Int)

Set the gravity for this grid.

open Unit
setHorizontalSpacing(horizontalSpacing: Int)

Set the amount of horizontal (x) spacing to place between each item in the grid.

open Unit
setNumColumns(numColumns: Int)

Set the number of columns in the grid

open Unit

Sets up this AbsListView to use a remote views adapter which connects to a RemoteViewsService through the specified intent.

open Unit
setSelection(position: Int)

Sets the currently selected item

open Unit
setStretchMode(stretchMode: Int)

Control how items are stretched to fill their space.

open Unit
setVerticalSpacing(verticalSpacing: Int)

Set the amount of vertical (y) spacing to place between each item in the grid.

open Unit

Smoothly scroll to the specified adapter position offset.

open Unit

Smoothly scroll to the specified adapter position.

Protected methods
open Unit
attachLayoutAnimationParameters(child: View!, params: ViewGroup.LayoutParams!, index: Int, count: Int)

open Int

open Int

open Int

open Unit

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

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

Inherited functions
Inherited properties

XML attributes

android:columnWidth

android:columnWidth
Specifies the fixed width for each column.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:gravity

android:gravity
Specifies the gravity within each cell.

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:horizontalSpacing

android:horizontalSpacing
Defines the default horizontal spacing between columns.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:numColumns

android:numColumns
Defines how many columns to show.

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

Must be one of the following constant values.

Constant Value Description
auto_fit ffffffff Display as many columns as possible to fill the available space.

android:stretchMode

android:stretchMode
Defines how columns should stretch to fill the available empty space, if any.

Must be one of the following constant values.

Constant Value Description
columnWidth 2 Each column is stretched equally.
none 0 Stretching is disabled.
spacingWidth 1 The spacing between each column is stretched.
spacingWidthUniform 3 The spacing between each column is uniformly stretched..

android:verticalSpacing

android:verticalSpacing
Defines the default vertical spacing between rows.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Constants

AUTO_FIT

Added in API level 1
static val AUTO_FIT: Int

Creates as many columns as can fit on screen.

Value: -1

NO_STRETCH

Added in API level 1
static val NO_STRETCH: Int

Disables stretching.

Value: 0

STRETCH_COLUMN_WIDTH

Added in API level 1
static val STRETCH_COLUMN_WIDTH: Int

Stretches columns.

Value: 2

STRETCH_SPACING

Added in API level 1
static val STRETCH_SPACING: Int

Stretches the spacing between columns.

Value: 1

STRETCH_SPACING_UNIFORM

Added in API level 3
static val STRETCH_SPACING_UNIFORM: Int

Stretches the spacing between columns. The spacing is uniform.

Value: 3

Public constructors

GridView

Added in API level 1
GridView(context: Context!)

GridView

Added in API level 1
GridView(
    context: Context!,
    attrs: AttributeSet!)

GridView

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

GridView

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

Public methods

getAccessibilityClassName

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

getAdapter

Added in API level 1
open fun getAdapter(): ListAdapter!
Return
ListAdapter! The adapter used to provide this view's content.

getColumnWidth

Added in API level 16
open fun getColumnWidth(): Int

Return the width of a column in the grid.

This may not be valid yet if a layout is pending.

Return
Int The column width in pixels

getGravity

Added in API level 16
open fun getGravity(): Int

Describes how the child views are horizontally aligned. Defaults to Gravity.LEFT

Return
Int the gravity that will be applied to this grid's children

getHorizontalSpacing

Added in API level 16
open fun getHorizontalSpacing(): Int

Returns the amount of horizontal spacing currently used between each item in the grid.

This is only accurate for the current layout. If setHorizontalSpacing(int) has been called but layout is not yet complete, this method may return a stale value. To get the horizontal spacing that was explicitly requested use getRequestedHorizontalSpacing().

Return
Int Current horizontal spacing between each item in pixels

getNumColumns

Added in API level 11
open fun getNumColumns(): Int

Get the number of columns in the grid. Returns AUTO_FIT if the Grid has never been laid out.

getRequestedColumnWidth

Added in API level 16
open fun getRequestedColumnWidth(): Int

Return the requested width of a column in the grid.

This may not be the actual column width used. Use getColumnWidth() to retrieve the current real width of a column.

Return
Int The requested column width in pixels

getRequestedHorizontalSpacing

Added in API level 16
open fun getRequestedHorizontalSpacing(): Int

Returns the requested amount of horizontal spacing between each item in the grid.

The value returned may have been supplied during inflation as part of a style, the default GridView style, or by a call to setHorizontalSpacing(int). If layout is not yet complete or if GridView calculated a different horizontal spacing from what was requested, this may return a different value from getHorizontalSpacing().

Return
Int The currently requested horizontal spacing between items, in pixels

getVerticalSpacing

Added in API level 16
open fun getVerticalSpacing(): Int

Returns the amount of vertical spacing between each item in the grid.

Return
Int The vertical spacing between items in pixels

onInitializeAccessibilityNodeInfoForItem

Added in API level 19
open fun onInitializeAccessibilityNodeInfoForItem(
    view: View!,
    position: Int,
    info: AccessibilityNodeInfo!
): Unit
Parameters
view View!: View representing the list item.
position Int: Position of the list item within the adapter.
info AccessibilityNodeInfo!: Node info to populate.

onKeyDown

Added in API level 1
open fun onKeyDown(
    keyCode: Int,
    event: KeyEvent!
): Boolean
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.

onKeyMultiple

Added in API level 1
open fun onKeyMultiple(
    keyCode: Int,
    repeatCount: Int,
    event: KeyEvent!
): Boolean
Parameters
keyCode Int: A key code that represents the button pressed, from android.view.KeyEvent.
count Number of pairs as returned by event.getRepeatCount().
event KeyEvent!: The KeyEvent object that defines the button action.
repeatCount Int: The number of times the action was made.
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
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.

setAdapter

Added in API level 1
open fun setAdapter(adapter: ListAdapter!): Unit

Sets the data behind this GridView.

Parameters
adapter ListAdapter!: the adapter providing the grid's data

setColumnWidth

Added in API level 1
open fun setColumnWidth(columnWidth: Int): Unit

Set the width of columns in the grid.

Parameters
columnWidth Int: The column width, in pixels.

setGravity

Added in API level 1
open fun setGravity(gravity: Int): Unit

Set the gravity for this grid. Gravity describes how the child views are horizontally aligned. Defaults to Gravity.LEFT

Parameters
gravity Int: the gravity to apply to this grid's children

setHorizontalSpacing

Added in API level 1
open fun setHorizontalSpacing(horizontalSpacing: Int): Unit

Set the amount of horizontal (x) spacing to place between each item in the grid.

Parameters
horizontalSpacing Int: The amount of horizontal space between items, in pixels.

setNumColumns

Added in API level 1
open fun setNumColumns(numColumns: Int): Unit

Set the number of columns in the grid

Parameters
numColumns Int: The desired number of columns.

setRemoteViewsAdapter

Added in API level 11
open fun setRemoteViewsAdapter(intent: Intent!): Unit

Sets up this AbsListView to use a remote views adapter which connects to a RemoteViewsService through the specified intent.

Parameters
intent Intent!: the intent used to identify the RemoteViewsService for the adapter to connect to.

setSelection

Added in API level 1
open fun setSelection(position: Int): Unit

Sets the currently selected item

Parameters
position Int: Index (starting at 0) of the data item to be selected. If in touch mode, the item will not be selected but it will still be positioned appropriately.

setStretchMode

Added in API level 1
open fun setStretchMode(stretchMode: Int): Unit

Control how items are stretched to fill their space.

Parameters
stretchMode Int: Either NO_STRETCH, STRETCH_SPACING, STRETCH_SPACING_UNIFORM, or STRETCH_COLUMN_WIDTH. Value is android.widget.GridView#NO_STRETCH, android.widget.GridView#STRETCH_SPACING, android.widget.GridView#STRETCH_COLUMN_WIDTH, or android.widget.GridView#STRETCH_SPACING_UNIFORM

setVerticalSpacing

Added in API level 1
open fun setVerticalSpacing(verticalSpacing: Int): Unit

Set the amount of vertical (y) spacing to place between each item in the grid.

Parameters
verticalSpacing Int: The amount of vertical space between items, in pixels.

smoothScrollByOffset

Added in API level 11
open fun smoothScrollByOffset(offset: Int): Unit

Smoothly scroll to the specified adapter position offset. The view will scroll such that the indicated position is displayed.

Parameters
offset Int: The amount to offset from the adapter position to scroll to.

smoothScrollToPosition

Added in API level 8
open fun smoothScrollToPosition(position: Int): Unit

Smoothly scroll to the specified adapter position. The view will scroll such that the indicated position is displayed.

Parameters
position Int: Scroll to this adapter position.

Protected methods

attachLayoutAnimationParameters

Added in API level 1
protected open fun attachLayoutAnimationParameters(
    child: View!,
    params: ViewGroup.LayoutParams!,
    index: Int,
    count: Int
): Unit
Parameters
child View!: the child to associate with animation parameters
params ViewGroup.LayoutParams!: the child's layout parameters which hold the animation parameters
index Int: the index of the child in the view group
count Int: the number of children in the view group

computeVerticalScrollExtent

Added in API level 1
protected open fun computeVerticalScrollExtent(): Int
Return
Int the vertical extent of the scrollbar's thumb

computeVerticalScrollOffset

Added in API level 1
protected open fun computeVerticalScrollOffset(): Int
Return
Int the vertical offset of the scrollbar's thumb

computeVerticalScrollRange

Added in API level 1
protected open fun computeVerticalScrollRange(): Int
Return
Int the total vertical range represented by the vertical scrollbar

The default range is the drawing height of this view.

layoutChildren

Added in API level 1
protected open fun layoutChildren(): Unit

onFocusChanged

Added in API level 1
protected open fun onFocusChanged(
    gainFocus: Boolean,
    direction: Int,
    previouslyFocusedRect: Rect?
): Unit
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 android.view.View#FOCUS_BACKWARD, android.view.View#FOCUS_FORWARD, android.view.View#FOCUS_LEFT, android.view.View#FOCUS_UP, android.view.View#FOCUS_RIGHT, or android.view.View#FOCUS_DOWN
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.

onMeasure

Added in API level 1
protected open fun onMeasure(
    widthMeasureSpec: Int,
    heightMeasureSpec: Int
): Unit
Parameters
widthMeasureSpec Int: horizontal space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.
heightMeasureSpec Int: vertical space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.