Added in API level 1

LinearLayout


open class LinearLayout : ViewGroup
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ android.widget.LinearLayout

A layout that arranges other views either horizontally in a single column or vertically in a single row.

The following snippet shows how to include a linear layout in your layout XML file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="horizontal"
    android:gravity="center">
 
    <!-- Include other widget or layout tags here. These are considered
            "child views" or "children" of the linear layout -->
 
  </LinearLayout>

Set android:orientation to specify whether child views are displayed in a row or column.

To control how linear layout aligns all the views it contains, set a value for android:gravity. For example, the snippet above sets android:gravity to "center". The value you set affects both horizontal and vertical alignment of all child views within the single row or column.

You can set android:layout_weight on individual child views to specify how linear layout divides remaining space amongst the views it contains. See the Linear Layout guide for an example.

See LinearLayout.LayoutParams to learn about other attributes you can set on a child view to affect its position and size in the containing linear layout.

Summary

Nested classes
open

Per-child layout information associated with ViewLinearLayout.

XML attributes
android:baselineAligned When set to false, prevents the layout from aligning its children's baselines.
android:baselineAlignedChildIndex When a linear layout is part of another layout that is baseline aligned, it can specify which of its children to baseline align to (that is, which child TextView).
android:gravity Specifies how an object should position its content, on both the X and Y axes, within its own bounds.
android:measureWithLargestChild When set to true, all children with a weight will be considered having the minimum size of the largest child.
android:orientation Should the layout be a column or a row? Use "horizontal" for a row, "vertical" for a column.
android:weightSum Defines the maximum weight sum.
Inherited XML attributes
Constants
static Int

static Int

Show a divider at the beginning of the group.

static Int

Show a divider at the end of the group.

static Int

Show dividers between each item in the group.

static Int

Don't show any dividers.

static Int

Inherited constants
Public constructors
LinearLayout(context: Context!)

LinearLayout(context: Context!, attrs: AttributeSet?)

LinearLayout(context: Context!, attrs: AttributeSet?, defStyleAttr: Int)

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

Public methods
open LinearLayout.LayoutParams!

open CharSequence!

open Int

open Int

open Drawable!

open Int

Get the padding size used to inset dividers in pixels

open Int

Returns the current gravity.

open Int

Returns the current orientation.

open Int

open Float

Returns the desired weights sum.

open Boolean

Indicates whether widgets contained within this layout are aligned on their baseline or not.

open Boolean

When true, all children with a weight will be considered having the minimum size of the largest child.

open Unit
onRtlPropertiesChanged(layoutDirection: Int)

Called when any RTL property (layout direction or text direction or text alignment) has been changed.

open Unit
setBaselineAligned(baselineAligned: Boolean)

Defines whether widgets contained in this layout are baseline-aligned or not.

open Unit

open Unit

Set a drawable to be used as a divider between items.

open Unit

Set padding displayed on both ends of dividers.

open Unit
setGravity(gravity: Int)

Describes how the child views are positioned.

open Unit
setHorizontalGravity(horizontalGravity: Int)

open Unit

When set to true, all children with a weight will be considered having the minimum size of the largest child.

open Unit
setOrientation(orientation: Int)

Should the layout be a column or a row.

open Unit
setShowDividers(showDividers: Int)

Set how dividers should be shown between items in this layout

open Unit
setVerticalGravity(verticalGravity: Int)

open Unit
setWeightSum(weightSum: Float)

Defines the desired weights sum.

open Boolean

Protected methods
open Boolean

open LinearLayout.LayoutParams!

Returns a set of layout parameters with a width of android.view.ViewGroup.LayoutParams#MATCH_PARENT and a height of android.view.ViewGroup.LayoutParams#WRAP_CONTENT when the layout's orientation is VERTICAL.

open LinearLayout.LayoutParams!

open Unit
onDraw(canvas: Canvas)

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

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

Inherited functions
Inherited properties

XML attributes

android:baselineAligned

android:baselineAligned
When set to false, prevents the layout from aligning its children's baselines. This attribute is particularly useful when the children use different values for gravity. The default value is true.

May be a boolean value, such as "true" or "false".

android:baselineAlignedChildIndex

android:baselineAlignedChildIndex
When a linear layout is part of another layout that is baseline aligned, it can specify which of its children to baseline align to (that is, which child TextView).

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

android:measureWithLargestChild
When set to true, all children with a weight will be considered having the minimum size of the largest child. If false, all children are measured normally.

May be a boolean value, such as "true" or "false".

android:orientation

android:orientation
Should the layout be a column or a row? Use "horizontal" for a row, "vertical" for a column. The default is horizontal.

Must be one of the following constant values.

Constant Value Description
horizontal 0 Defines an horizontal widget.
vertical 1 Defines a vertical widget.

android:weightSum

android:weightSum
Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

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

Constants

HORIZONTAL

Added in API level 1
static val HORIZONTAL: Int
Value: 0

SHOW_DIVIDER_BEGINNING

Added in API level 11
static val SHOW_DIVIDER_BEGINNING: Int

Show a divider at the beginning of the group.

Value: 1

SHOW_DIVIDER_END

Added in API level 11
static val SHOW_DIVIDER_END: Int

Show a divider at the end of the group.

Value: 4

SHOW_DIVIDER_MIDDLE

Added in API level 11
static val SHOW_DIVIDER_MIDDLE: Int

Show dividers between each item in the group.

Value: 2

SHOW_DIVIDER_NONE

Added in API level 11
static val SHOW_DIVIDER_NONE: Int

Don't show any dividers.

Value: 0

VERTICAL

Added in API level 1
static val VERTICAL: Int
Value: 1

Public constructors

LinearLayout

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

LinearLayout

Added in API level 1
LinearLayout(
    context: Context!,
    attrs: AttributeSet?)
Parameters
attrs AttributeSet?: This value may be null.

LinearLayout

Added in API level 11
LinearLayout(
    context: Context!,
    attrs: AttributeSet?,
    defStyleAttr: Int)
Parameters
attrs AttributeSet?: This value may be null.

LinearLayout

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

Public methods

generateLayoutParams

Added in API level 1
open fun generateLayoutParams(attrs: AttributeSet!): LinearLayout.LayoutParams!
Parameters
attrs AttributeSet!: the attributes to build the layout parameters from
Return
LinearLayout.LayoutParams! an instance of android.view.ViewGroup.LayoutParams or one of its descendants

getAccessibilityClassName

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

getBaseline

Added in API level 1
open fun getBaseline(): Int
Return
Int the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported

getBaselineAlignedChildIndex

Added in API level 1
open fun getBaselineAlignedChildIndex(): Int
Return
Int The index of the child that will be used if this layout is part of a larger layout that is baseline aligned, or -1 if none has been set.

getDividerDrawable

Added in API level 16
open fun getDividerDrawable(): Drawable!
Return
Drawable! the divider Drawable that will divide each item.

getDividerPadding

Added in API level 14
open fun getDividerPadding(): Int

Get the padding size used to inset dividers in pixels

getGravity

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

Returns the current gravity. See android.view.Gravity

Return
Int the current gravity.

See Also

getOrientation

Added in API level 1
open fun getOrientation(): Int

Returns the current orientation.

Return
Int either HORIZONTAL or VERTICAL Value is android.widget.LinearLayout#HORIZONTAL, or android.widget.LinearLayout#VERTICAL

getShowDividers

Added in API level 11
open fun getShowDividers(): Int
Return
Int A flag set indicating how dividers should be shown around items. Value is either 0 or a combination of android.widget.LinearLayout#SHOW_DIVIDER_NONE, android.widget.LinearLayout#SHOW_DIVIDER_BEGINNING, android.widget.LinearLayout#SHOW_DIVIDER_MIDDLE, and android.widget.LinearLayout#SHOW_DIVIDER_END

getWeightSum

Added in API level 1
open fun getWeightSum(): Float

Returns the desired weights sum.

Return
Float A number greater than 0.0f if the weight sum is defined, or a number lower than or equals to 0.0f if not weight sum is to be used.

isBaselineAligned

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

Indicates whether widgets contained within this layout are aligned on their baseline or not.

Return
Boolean true when widgets are baseline-aligned, false otherwise

isMeasureWithLargestChildEnabled

Added in API level 11
open fun isMeasureWithLargestChildEnabled(): Boolean

When true, all children with a weight will be considered having the minimum size of the largest child. If false, all children are measured normally.

Return
Boolean True to measure children with a weight using the minimum size of the largest child, false otherwise.

onRtlPropertiesChanged

Added in API level 17
open fun onRtlPropertiesChanged(layoutDirection: Int): Unit

Called when any RTL property (layout direction or text direction or text alignment) has been changed. Subclasses need to override this method to take care of cached information that depends on the resolved layout direction, or to inform child views that inherit their layout direction. The default implementation does nothing.

Parameters
layoutDirection Int: Value is android.view.View#LAYOUT_DIRECTION_LTR, or android.view.View#LAYOUT_DIRECTION_RTL

setBaselineAligned

Added in API level 1
open fun setBaselineAligned(baselineAligned: Boolean): Unit

Defines whether widgets contained in this layout are baseline-aligned or not.

Parameters
baselineAligned Boolean: true to align widgets on their baseline, false otherwise

setBaselineAlignedChildIndex

Added in API level 1
open fun setBaselineAlignedChildIndex(i: Int): Unit
Parameters
i Int: The index of the child that will be used if this layout is part of a larger layout that is baseline aligned.

setDividerDrawable

Added in API level 11
open fun setDividerDrawable(divider: Drawable!): Unit

Set a drawable to be used as a divider between items.

Parameters
divider Drawable!: Drawable that will divide each item.

setDividerPadding

Added in API level 14
open fun setDividerPadding(padding: Int): Unit

Set padding displayed on both ends of dividers. For a vertical layout, the padding is applied to left and right end of dividers. For a horizontal layout, the padding is applied to top and bottom end of dividers.

Parameters
padding Int: Padding value in pixels that will be applied to each end

setGravity

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

Describes how the child views are positioned. Defaults to GRAVITY_TOP. If this layout has a VERTICAL orientation, this controls where all the child views are placed if there is extra vertical space. If this layout has a HORIZONTAL orientation, this controls the alignment of the children.

Parameters
gravity Int: See android.view.Gravity

setHorizontalGravity

Added in API level 1
open fun setHorizontalGravity(horizontalGravity: Int): Unit

setMeasureWithLargestChildEnabled

Added in API level 11
open fun setMeasureWithLargestChildEnabled(enabled: Boolean): Unit

When set to true, all children with a weight will be considered having the minimum size of the largest child. If false, all children are measured normally. Disabled by default.

Parameters
enabled Boolean: True to measure children with a weight using the minimum size of the largest child, false otherwise.

setOrientation

Added in API level 1
open fun setOrientation(orientation: Int): Unit

Should the layout be a column or a row.

Parameters
orientation Int: Pass HORIZONTAL or VERTICAL. Default value is HORIZONTAL. Value is android.widget.LinearLayout#HORIZONTAL, or android.widget.LinearLayout#VERTICAL

setShowDividers

Added in API level 11
open fun setShowDividers(showDividers: Int): Unit

Set how dividers should be shown between items in this layout

Parameters
showDividers Int: One or more of SHOW_DIVIDER_BEGINNING, SHOW_DIVIDER_MIDDLE, or SHOW_DIVIDER_END to show dividers, or SHOW_DIVIDER_NONE to show no dividers. Value is either 0 or a combination of android.widget.LinearLayout#SHOW_DIVIDER_NONE, android.widget.LinearLayout#SHOW_DIVIDER_BEGINNING, android.widget.LinearLayout#SHOW_DIVIDER_MIDDLE, and android.widget.LinearLayout#SHOW_DIVIDER_END

setVerticalGravity

Added in API level 1
open fun setVerticalGravity(verticalGravity: Int): Unit

setWeightSum

Added in API level 1
open fun setWeightSum(weightSum: Float): Unit

Defines the desired weights sum. If unspecified the weights sum is computed at layout time by adding the layout_weight of each child. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

Parameters
weightSum Float: a number greater than 0.0f, or a number lower than or equals to 0.0f if the weight sum should be computed from the children's layout_weight

shouldDelayChildPressedState

Added in API level 14
open fun shouldDelayChildPressedState(): Boolean

Protected methods

checkLayoutParams

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

generateDefaultLayoutParams

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

Returns a set of layout parameters with a width of android.view.ViewGroup.LayoutParams#MATCH_PARENT and a height of android.view.ViewGroup.LayoutParams#WRAP_CONTENT when the layout's orientation is VERTICAL. When the orientation is HORIZONTAL, the width is set to LayoutParams#WRAP_CONTENT and the height to LayoutParams#WRAP_CONTENT.

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

generateLayoutParams

Added in API level 1
protected open fun generateLayoutParams(lp: ViewGroup.LayoutParams!): LinearLayout.LayoutParams!
Parameters
p The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Return
LinearLayout.LayoutParams! an instance of android.view.ViewGroup.LayoutParams or one of its descendants

onDraw

Added in API level 1
protected open fun onDraw(canvas: Canvas): Unit
Parameters
canvas Canvas: the canvas on which the background will be drawn This value cannot be null.

onLayout

Added in API level 1
protected open fun onLayout(
    changed: Boolean,
    l: Int,
    t: Int,
    r: Int,
    b: Int
): Unit
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

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.