Class LinearLayout

All Implemented Interfaces:
Drawable.Callback, ViewManager, ViewParent
Direct Known Subclasses:
ListMenuItemView, RadioGroup, TableLayout, TableRow, TestFragment.TestLinearLayout

public class LinearLayout extends ViewGroup
A layout that arranges other views either horizontally in a single column or vertically in a single row.

Set setOrientation(int) 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 setGravity(int). 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 LinearLayout.LayoutParams.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.

Since:
2.0
  • Field Details

    • HORIZONTAL

      public static final int HORIZONTAL
      Orientation horizontal.
      See Also:
    • VERTICAL

      public static final int VERTICAL
      Orientation vertical.
      See Also:
    • SHOW_DIVIDER_NONE

      public static final int SHOW_DIVIDER_NONE
      Don't show any dividers.
      See Also:
    • SHOW_DIVIDER_BEGINNING

      public static final int SHOW_DIVIDER_BEGINNING
      Show a divider at the beginning of the group.
      See Also:
    • SHOW_DIVIDER_MIDDLE

      public static final int SHOW_DIVIDER_MIDDLE
      Show dividers between each item in the group.
      See Also:
    • SHOW_DIVIDER_END

      public static final int SHOW_DIVIDER_END
      Show a divider at the end of the group.
      See Also:
  • Constructor Details

    • LinearLayout

      public LinearLayout(Context context)
  • Method Details

    • setShowDividers

      public void setShowDividers(int showDividers)
      Set how dividers should be shown between items in this layout
      Parameters:
      showDividers - 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.
    • getShowDividers

      public int getShowDividers()
      Returns:
      A flag set indicating how dividers should be shown around items.
      See Also:
    • getDividerDrawable

      @Nullable public Drawable getDividerDrawable()
      Returns:
      the divider Drawable that will divide each item.
      See Also:
    • setDividerDrawable

      public void setDividerDrawable(@Nullable Drawable divider)
      Set a drawable to be used as a divider between items.
      Parameters:
      divider - Drawable that will divide each item.
      See Also:
    • setDividerPadding

      public void setDividerPadding(int padding)
      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 - Padding value in pixels that will be applied to each end
      See Also:
    • getDividerPadding

      public int getDividerPadding()
      Get the padding size used to inset dividers in pixels
      See Also:
    • getDividerWidth

      public int getDividerWidth()
      Get the width of the current divider drawable.
    • isBaselineAligned

      public boolean isBaselineAligned()

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

      Returns:
      true when widgets are baseline-aligned, false otherwise
    • setBaselineAligned

      public void setBaselineAligned(boolean baselineAligned)

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

      Parameters:
      baselineAligned - true to align widgets on their baseline, false otherwise
    • isMeasureWithLargestChildEnabled

      public boolean isMeasureWithLargestChildEnabled()
      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.
      Returns:
      True to measure children with a weight using the minimum size of the largest child, false otherwise.
    • setMeasureWithLargestChildEnabled

      public void setMeasureWithLargestChildEnabled(boolean enabled)
      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 - True to measure children with a weight using the minimum size of the largest child, false otherwise.
    • getBaseline

      public int getBaseline()
      Description copied from class: View

      Return the offset of the widget's text baseline from the widget's top boundary. If this widget does not support baseline alignment, this method returns -1.

      Overrides:
      getBaseline in class View
      Returns:
      the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported
    • getBaselineAlignedChildIndex

      public int getBaselineAlignedChildIndex()
      Returns:
      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.
    • setBaselineAlignedChildIndex

      public void setBaselineAlignedChildIndex(int i)
      Parameters:
      i - The index of the child that will be used if this layout is part of a larger layout that is baseline aligned.
    • getWeightSum

      public float getWeightSum()
      Returns the desired weights sum.
      Returns:
      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.
    • setWeightSum

      public void setWeightSum(float weightSum)
      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 - 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
    • onDraw

      protected void onDraw(@Nonnull Canvas canvas)
      Description copied from class: View
      Draw the content of this view, implement this to do your drawing.

      Note that (0, 0) will be the top left of the bounds, and (width, height) will be the bottom right of the bounds.

      Overrides:
      onDraw in class View
      Parameters:
      canvas - the canvas to draw content
    • onMeasure

      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
      Description copied from class: View
      Measure the view and its content to determine the measured width and the measured height. This method is invoked by View.measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

      CONTRACT: When overriding this method, you must call View.setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by View.measure(int, int). Calling super.onMeasure() is a valid use.

      The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override the base one to provide better measurements of their content.

      Overrides:
      onMeasure in class View
      Parameters:
      widthMeasureSpec - width measure specification imposed by the parent MeasureSpec
      heightMeasureSpec - height measure specification imposed by the parent MeasureSpec
    • hasDividerBeforeChildAt

      protected boolean hasDividerBeforeChildAt(int childIndex)
      Determines where to position dividers between children.
      Parameters:
      childIndex - Index of child to check for preceding divider
      Returns:
      true if there should be a divider before the child at childIndex
    • onLayout

      protected void onLayout(boolean changed, int left, int top, int right, int bottom)
      Description copied from class: View
      Called from View.layout(int, int, int, int) 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.

      Specified by:
      onLayout in class ViewGroup
      Parameters:
      changed - 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
    • onRtlPropertiesChanged

      public void onRtlPropertiesChanged(int layoutDirection)
      Description copied from class: View
      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.

      Overrides:
      onRtlPropertiesChanged in class View
      Parameters:
      layoutDirection - the direction of the layout
      See Also:
    • setOrientation

      public void setOrientation(int orientation)
      Should the layout be a column or a row.
      Parameters:
      orientation - Pass HORIZONTAL or VERTICAL. Default value is HORIZONTAL.
    • getOrientation

      public int getOrientation()
      Returns the current orientation.
      Returns:
      either HORIZONTAL or VERTICAL
    • setGravity

      public void setGravity(int gravity)
      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 - See Gravity
    • getGravity

      public int getGravity()
      Returns the current gravity. See Gravity
      Returns:
      the current gravity.
      See Also:
    • setHorizontalGravity

      public void setHorizontalGravity(int horizontalGravity)
    • setVerticalGravity

      public void setVerticalGravity(int verticalGravity)
    • generateLayoutParams

      @Nonnull protected ViewGroup.LayoutParams generateLayoutParams(@Nonnull ViewGroup.LayoutParams params)
      Description copied from class: ViewGroup
      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 ViewGroup.checkLayoutParams(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.
      Overrides:
      generateLayoutParams in class ViewGroup
      Parameters:
      params - The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
      Returns:
      an instance of ViewGroup.LayoutParams or one of its descendants
    • generateDefaultLayoutParams

      @Nonnull protected ViewGroup.LayoutParams generateDefaultLayoutParams()
      Returns a set of layout parameters with a width of ViewGroup.LayoutParams.MATCH_PARENT and a height of ViewGroup.LayoutParams.WRAP_CONTENT when the layout's orientation is VERTICAL. When the orientation is HORIZONTAL, the width is set to ViewGroup.LayoutParams.WRAP_CONTENT and the height to ViewGroup.LayoutParams.WRAP_CONTENT.
      Overrides:
      generateDefaultLayoutParams in class ViewGroup
      Returns:
      a set of default layout parameters
    • checkLayoutParams

      protected boolean checkLayoutParams(@Nullable ViewGroup.LayoutParams params)
      Description copied from class: ViewGroup
      Check whether given params fit to this view group.

      See also ViewGroup.generateLayoutParams(LayoutParams) See also ViewGroup.generateDefaultLayoutParams()

      Overrides:
      checkLayoutParams in class ViewGroup
      Parameters:
      params - layout params to check
      Returns:
      if params matched to this view group