Google is committed to advancing racial equity for Black communities. See how.
Added in API level 1

Layout

abstract class Layout
kotlin.Any
   ↳ android.text.Layout

A base class that manages text layout in visual elements on the screen.

For text that will be edited, use a DynamicLayout, which will be updated as the text changes. For text that will not change, use a StaticLayout.

Summary

Nested classes

open

Stores information about bidirectional (left-to-right or right-to-left) text within the layout of a line.

Constants
static Int

Value for break strategy indicating balanced line breaking.

static Int

Value for break strategy indicating high quality line breaking, including automatic hyphenation and doing whole-paragraph optimization of line breaks.

static Int

Value for break strategy indicating simple line breaking.

static Float

static Float

static Int

static Int

static Int

Value for hyphenation frequency indicating the full amount of automatic hyphenation, typical in typography.

static Int

Value for hyphenation frequency indicating no automatic hyphenation.

static Int

Value for hyphenation frequency indicating a light amount of automatic hyphenation, which is a conservative default.

static Int

Value for justification mode indicating the text is justified by stretching word spacing.

static Int

Value for justification mode indicating no justification.

Protected constructors
<init>(text: CharSequence!, paint: TextPaint!, width: Int, align: Layout.Alignment!, spacingMult: Float, spacingAdd: Float)

Subclasses of Layout use this constructor to set the display text, width, and other standard properties.

Public methods
open Unit
draw(c: Canvas!)

Draw this Layout on the specified Canvas.

open Unit
draw(canvas: Canvas!, highlight: Path!, highlightPaint: Paint!, cursorOffsetVertical: Int)

Draw this Layout on the specified canvas, with the highlight path drawn between the background and the text.

Layout.Alignment!

Return the base alignment of this layout.

abstract Int

Returns the number of extra pixels of descent padding in the bottom line of the Layout.

open Unit
getCursorPath(point: Int, dest: Path!, editingBuffer: CharSequence!)

Fills in the specified Path with a representation of a cursor at the specified offset.

open static Float

Return how wide a layout must be in order to display the specified text with one line per paragraph.

open static Float
getDesiredWidth(source: CharSequence!, start: Int, end: Int, paint: TextPaint!)

Return how wide a layout must be in order to display the specified text slice with one line per paragraph.

abstract Int

Returns the number of characters to be ellipsized away, or 0 if no ellipsis is to take place.

abstract Int

Return the offset of the first character to be ellipsized away, relative to the start of the line.

open Int

Return the width to which this Layout is ellipsizing, or getWidth if it is not doing anything special.

open Int

Return the total height of this layout.

Int

Get the ascent of the text on the specified line.

Int

Return the vertical position of the baseline of the specified line.

Int

Return the vertical position of the bottom of the specified line.

open Int
getLineBounds(line: Int, bounds: Rect!)

Return the baseline for the specified line (0…getLineCount() - 1) If bounds is not null, return the top, left, right, bottom extents of the specified line in it.

abstract Boolean

Returns whether the specified line contains one or more characters that need to be handled specially, like tabs.

abstract Int

Return the number of lines of text in this layout.

abstract Int

Return the descent of the specified line(0…getLineCount() - 1).

abstract Layout.Directions!

Returns the directional run information for the specified line.

Int
getLineEnd(line: Int)

Return the text offset after the last character on the specified line.

open Int

Get the line number on which the specified text offset appears.

open Int

Get the line number corresponding to the specified vertical position.

open Float

Get the leftmost position that should be exposed for horizontal scrolling on the specified line.

open Float
getLineMax(line: Int)

Gets the unsigned horizontal extent of the specified line, including leading margin indent, but excluding trailing whitespace.

open Float

Get the rightmost position that should be exposed for horizontal scrolling on the specified line.

abstract Int

Return the text offset of the beginning of the specified line ( 0…getLineCount()).

abstract Int
getLineTop(line: Int)

Return the vertical position of the top of the specified line (0…getLineCount()).

open Int

Return the text offset after the last visible character (so whitespace is not counted) on the specified line.

open Float

Gets the unsigned horizontal extent of the specified line, including leading margin indent and trailing whitespace.

open Int

Get the character offset on the specified line whose position is closest to the specified horizontal position.

open Int

open Int

TextPaint!

Return the base Paint properties for this layout.

Layout.Alignment!

Get the alignment of the specified paragraph, taking into account markup attached to it.

abstract Int

Returns the primary directionality of the paragraph containing the specified line, either 1 for left-to-right lines, or -1 for right-to-left lines (see DIR_LEFT_TO_RIGHT, DIR_RIGHT_TO_LEFT).

Int

Get the left edge of the specified paragraph, inset by left margins.

Int

Get the right edge of the specified paragraph, inset by right margins.

open Float

Get the primary horizontal position for the specified text offset.

open Float

Get the secondary horizontal position for the specified text offset.

open Unit
getSelectionPath(start: Int, end: Int, dest: Path!)

Fills in the specified Path with a representation of a highlight between the specified offsets.

Float

Return the number of units of leading that are added to each line.

Float

Return what the text height is multiplied by to get the line height.

CharSequence!

Return the text that is displayed by this Layout.

abstract Int

Returns the (negative) number of extra pixels of ascent padding in the top line of the Layout.

Int

Return the width of this layout.

Unit

Increase the width of this layout to the specified width.

open Boolean
isRtlCharAt(offset: Int)

Returns true if the character at offset is right to left (RTL).

Protected methods
Boolean

Constants

BREAK_STRATEGY_BALANCED

Added in API level 23
static val BREAK_STRATEGY_BALANCED: Int

Value for break strategy indicating balanced line breaking. The breaks are chosen to make all lines as close to the same length as possible, including automatic hyphenation.

Value: 2

BREAK_STRATEGY_HIGH_QUALITY

Added in API level 23
static val BREAK_STRATEGY_HIGH_QUALITY: Int

Value for break strategy indicating high quality line breaking, including automatic hyphenation and doing whole-paragraph optimization of line breaks.

Value: 1

BREAK_STRATEGY_SIMPLE

Added in API level 23
static val BREAK_STRATEGY_SIMPLE: Int

Value for break strategy indicating simple line breaking. Automatic hyphens are not added (though soft hyphens are respected), and modifying text generally doesn't affect the layout before it (which yields a more consistent user experience when editing), but layout may not be the highest quality.

Value: 0

DEFAULT_LINESPACING_ADDITION

Added in API level 28
static val DEFAULT_LINESPACING_ADDITION: Float
Value: 0.0f

DEFAULT_LINESPACING_MULTIPLIER

Added in API level 28
static val DEFAULT_LINESPACING_MULTIPLIER: Float
Value: 1.0f

DIR_LEFT_TO_RIGHT

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

DIR_RIGHT_TO_LEFT

Added in API level 1
static val DIR_RIGHT_TO_LEFT: Int
Value: -1

HYPHENATION_FREQUENCY_FULL

Added in API level 23
static val HYPHENATION_FREQUENCY_FULL: Int

Value for hyphenation frequency indicating the full amount of automatic hyphenation, typical in typography. Useful for running text and where it's important to put the maximum amount of text in a screen with limited space.

Value: 2

HYPHENATION_FREQUENCY_NONE

Added in API level 23
static val HYPHENATION_FREQUENCY_NONE: Int

Value for hyphenation frequency indicating no automatic hyphenation. Useful for backward compatibility, and for cases where the automatic hyphenation algorithm results in incorrect hyphenation. Mid-word breaks may still happen when a word is wider than the layout and there is otherwise no valid break. Soft hyphens are ignored and will not be used as suggestions for potential line breaks.

Value: 0

HYPHENATION_FREQUENCY_NORMAL

Added in API level 23
static val HYPHENATION_FREQUENCY_NORMAL: Int

Value for hyphenation frequency indicating a light amount of automatic hyphenation, which is a conservative default. Useful for informal cases, such as short sentences or chat messages.

Value: 1

JUSTIFICATION_MODE_INTER_WORD

Added in API level 26
static val JUSTIFICATION_MODE_INTER_WORD: Int

Value for justification mode indicating the text is justified by stretching word spacing.

Value: 1

JUSTIFICATION_MODE_NONE

Added in API level 26
static val JUSTIFICATION_MODE_NONE: Int

Value for justification mode indicating no justification.

Value: 0

Protected constructors

<init>

Added in API level 1
protected Layout(
    text: CharSequence!,
    paint: TextPaint!,
    width: Int,
    align: Layout.Alignment!,
    spacingMult: Float,
    spacingAdd: Float)

Subclasses of Layout use this constructor to set the display text, width, and other standard properties.

Parameters
text CharSequence!: the text to render
paint TextPaint!: the default paint for the layout. Styles can override various attributes of the paint.
width Int: the wrapping width for the text.
align Layout.Alignment!: whether to left, right, or center the text. Styles can override the alignment.
spacingMult Float: factor by which to scale the font size to get the default line spacing
spacingAdd Float: amount to add to the default line spacing

Public methods

draw

Added in API level 1
open fun draw(c: Canvas!): Unit

Draw this Layout on the specified Canvas.

draw

Added in API level 1
open fun draw(
    canvas: Canvas!,
    highlight: Path!,
    highlightPaint: Paint!,
    cursorOffsetVertical: Int
): Unit

Draw this Layout on the specified canvas, with the highlight path drawn between the background and the text.

Parameters
canvas Canvas!: the canvas
highlight Path!: the path of the highlight or cursor; can be null
highlightPaint Paint!: the paint for the highlight
cursorOffsetVertical Int: the amount to temporarily translate the canvas while rendering the highlight

getAlignment

Added in API level 1
fun getAlignment(): Layout.Alignment!

Return the base alignment of this layout.

getBottomPadding

Added in API level 1
abstract fun getBottomPadding(): Int

Returns the number of extra pixels of descent padding in the bottom line of the Layout.

getCursorPath

Added in API level 1
open fun getCursorPath(
    point: Int,
    dest: Path!,
    editingBuffer: CharSequence!
): Unit

Fills in the specified Path with a representation of a cursor at the specified offset. This will often be a vertical line but can be multiple discontinuous lines in text with multiple directionalities.

getDesiredWidth

Added in API level 1
open static fun getDesiredWidth(
    source: CharSequence!,
    paint: TextPaint!
): Float

Return how wide a layout must be in order to display the specified text with one line per paragraph.

As of O, Uses TextDirectionHeuristics#FIRSTSTRONG_LTR as the default text direction heuristics. In the earlier versions uses TextDirectionHeuristics#LTR as the default.

getDesiredWidth

Added in API level 1
open static fun getDesiredWidth(
    source: CharSequence!,
    start: Int,
    end: Int,
    paint: TextPaint!
): Float

Return how wide a layout must be in order to display the specified text slice with one line per paragraph.

As of O, Uses TextDirectionHeuristics#FIRSTSTRONG_LTR as the default text direction heuristics. In the earlier versions uses TextDirectionHeuristics#LTR as the default.

getEllipsisCount

Added in API level 1
abstract fun getEllipsisCount(line: Int): Int

Returns the number of characters to be ellipsized away, or 0 if no ellipsis is to take place.

getEllipsisStart

Added in API level 1
abstract fun getEllipsisStart(line: Int): Int

Return the offset of the first character to be ellipsized away, relative to the start of the line. (So 0 if the beginning of the line is ellipsized, not getLineStart().)

getEllipsizedWidth

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

Return the width to which this Layout is ellipsizing, or getWidth if it is not doing anything special.

getHeight

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

Return the total height of this layout.

getLineAscent

Added in API level 1
fun getLineAscent(line: Int): Int

Get the ascent of the text on the specified line. The return value is negative to match the Paint.ascent() convention.

getLineBaseline

Added in API level 1
fun getLineBaseline(line: Int): Int

Return the vertical position of the baseline of the specified line.

getLineBottom

Added in API level 1
fun getLineBottom(line: Int): Int

Return the vertical position of the bottom of the specified line.

getLineBounds

Added in API level 1
open fun getLineBounds(
    line: Int,
    bounds: Rect!
): Int

Return the baseline for the specified line (0…getLineCount() - 1) If bounds is not null, return the top, left, right, bottom extents of the specified line in it.

Parameters
line Int: which line to examine (0..getLineCount() - 1)
bounds Rect!: Optional. If not null, it returns the extent of the line
Return
Int the Y-coordinate of the baseline

getLineContainsTab

Added in API level 1
abstract fun getLineContainsTab(line: Int): Boolean

Returns whether the specified line contains one or more characters that need to be handled specially, like tabs.

getLineCount

Added in API level 1
abstract fun getLineCount(): Int

Return the number of lines of text in this layout.

getLineDescent

Added in API level 1
abstract fun getLineDescent(line: Int): Int

Return the descent of the specified line(0…getLineCount() - 1).

getLineDirections

Added in API level 1
abstract fun getLineDirections(line: Int): Layout.Directions!

Returns the directional run information for the specified line. The array alternates counts of characters in left-to-right and right-to-left segments of the line.

NOTE: this is inadequate to support bidirectional text, and will change.

getLineEnd

Added in API level 1
fun getLineEnd(line: Int): Int

Return the text offset after the last character on the specified line.

getLineForOffset

Added in API level 1
open fun getLineForOffset(offset: Int): Int

Get the line number on which the specified text offset appears. If you ask for a position before 0, you get 0; if you ask for a position beyond the end of the text, you get the last line.

getLineForVertical

Added in API level 1
open fun getLineForVertical(vertical: Int): Int

Get the line number corresponding to the specified vertical position. If you ask for a position above 0, you get 0; if you ask for a position below the bottom of the text, you get the last line.

getLineLeft

Added in API level 1
open fun getLineLeft(line: Int): Float

Get the leftmost position that should be exposed for horizontal scrolling on the specified line.

getLineMax

Added in API level 1
open fun getLineMax(line: Int): Float

Gets the unsigned horizontal extent of the specified line, including leading margin indent, but excluding trailing whitespace.

getLineRight

Added in API level 1
open fun getLineRight(line: Int): Float

Get the rightmost position that should be exposed for horizontal scrolling on the specified line.

getLineStart

Added in API level 1
abstract fun getLineStart(line: Int): Int

Return the text offset of the beginning of the specified line ( 0…getLineCount()). If the specified line is equal to the line count, returns the length of the text.

getLineTop

Added in API level 1
abstract fun getLineTop(line: Int): Int

Return the vertical position of the top of the specified line (0…getLineCount()). If the specified line is equal to the line count, returns the bottom of the last line.

getLineVisibleEnd

Added in API level 1
open fun getLineVisibleEnd(line: Int): Int

Return the text offset after the last visible character (so whitespace is not counted) on the specified line.

getLineWidth

Added in API level 1
open fun getLineWidth(line: Int): Float

Gets the unsigned horizontal extent of the specified line, including leading margin indent and trailing whitespace.

getOffsetForHorizontal

Added in API level 1
open fun getOffsetForHorizontal(
    line: Int,
    horiz: Float
): Int

Get the character offset on the specified line whose position is closest to the specified horizontal position.

getOffsetToLeftOf

Added in API level 1
open fun getOffsetToLeftOf(offset: Int): Int

getOffsetToRightOf

Added in API level 1
open fun getOffsetToRightOf(offset: Int): Int

getPaint

Added in API level 1
fun getPaint(): TextPaint!

Return the base Paint properties for this layout. Do NOT change the paint, which may result in funny drawing for this layout.

getParagraphAlignment

Added in API level 1
fun getParagraphAlignment(line: Int): Layout.Alignment!

Get the alignment of the specified paragraph, taking into account markup attached to it.

getParagraphDirection

Added in API level 1
abstract fun getParagraphDirection(line: Int): Int

Returns the primary directionality of the paragraph containing the specified line, either 1 for left-to-right lines, or -1 for right-to-left lines (see DIR_LEFT_TO_RIGHT, DIR_RIGHT_TO_LEFT).

getParagraphLeft

Added in API level 1
fun getParagraphLeft(line: Int): Int

Get the left edge of the specified paragraph, inset by left margins.

getParagraphRight

Added in API level 1
fun getParagraphRight(line: Int): Int

Get the right edge of the specified paragraph, inset by right margins.

getPrimaryHorizontal

Added in API level 1
open fun getPrimaryHorizontal(offset: Int): Float

Get the primary horizontal position for the specified text offset. This is the location where a new character would be inserted in the paragraph's primary direction.

getSecondaryHorizontal

Added in API level 1
open fun getSecondaryHorizontal(offset: Int): Float

Get the secondary horizontal position for the specified text offset. This is the location where a new character would be inserted in the direction other than the paragraph's primary direction.

getSelectionPath

Added in API level 1
open fun getSelectionPath(
    start: Int,
    end: Int,
    dest: Path!
): Unit

Fills in the specified Path with a representation of a highlight between the specified offsets. This will often be a rectangle or a potentially discontinuous set of rectangles. If the start and end are the same, the returned path is empty.

getSpacingAdd

Added in API level 1
fun getSpacingAdd(): Float

Return the number of units of leading that are added to each line.

getSpacingMultiplier

Added in API level 1
fun getSpacingMultiplier(): Float

Return what the text height is multiplied by to get the line height.

getText

Added in API level 1
fun getText(): CharSequence!

Return the text that is displayed by this Layout.

getTopPadding

Added in API level 1
abstract fun getTopPadding(): Int

Returns the (negative) number of extra pixels of ascent padding in the top line of the Layout.

getWidth

Added in API level 1
fun getWidth(): Int

Return the width of this layout.

increaseWidthTo

Added in API level 1
fun increaseWidthTo(wid: Int): Unit

Increase the width of this layout to the specified width. Be careful to use this only when you know it is appropriate— it does not cause the text to reflow to use the full new width.

isRtlCharAt

Added in API level 14
open fun isRtlCharAt(offset: Int): Boolean

Returns true if the character at offset is right to left (RTL).

Parameters
offset Int: the offset
Return
Boolean true if the character is RTL, false if it is LTR

Protected methods

isSpanned

Added in API level 1
protected fun isSpanned(): Boolean