Added in API level 1

DynamicLayout


open class DynamicLayout : Layout

DynamicLayout is a text layout that updates itself as the text is edited.

This is used by widgets to control text layout. You should not need to use this class directly unless you are implementing your own widget or custom display object, or need to call Canvas.drawText() directly.

Summary

Nested classes

Builder for dynamic layouts.

Inherited constants
Int BREAK_STRATEGY_BALANCED

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.

Int BREAK_STRATEGY_HIGH_QUALITY

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

Int BREAK_STRATEGY_SIMPLE

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.

Float DEFAULT_LINESPACING_ADDITION

Float DEFAULT_LINESPACING_MULTIPLIER

Int DIR_LEFT_TO_RIGHT

Int DIR_RIGHT_TO_LEFT

Int HYPHENATION_FREQUENCY_FULL

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.

Int HYPHENATION_FREQUENCY_FULL_FAST

Value for hyphenation frequency indicating the full amount of automatic hyphenation with using faster algorithm. This option is useful for running text and where it's important to put the maximum amount of text in a screen with limited space. To make text rendering faster with hyphenation, this algorithm ignores some hyphen character related typographic features, e.g. kerning.

Int HYPHENATION_FREQUENCY_NONE

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.

Int HYPHENATION_FREQUENCY_NORMAL

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.

Int HYPHENATION_FREQUENCY_NORMAL_FAST

Value for hyphenation frequency indicating a light amount of automatic hyphenation with using faster algorithm. This option is useful for informal cases, such as short sentences or chat messages. To make text rendering faster with hyphenation, this algorithm ignores some hyphen character related typographic features, e.g. kerning.

Int JUSTIFICATION_MODE_INTER_CHARACTER

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

Int JUSTIFICATION_MODE_INTER_WORD

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

Int JUSTIFICATION_MODE_NONE

Value for justification mode indicating no justification.

Public constructors
DynamicLayout(base: CharSequence, paint: TextPaint, width: Int, align: Layout.Alignment, spacingmult: Float, spacingadd: Float, includepad: Boolean)

DynamicLayout(base: CharSequence, display: CharSequence, paint: TextPaint, width: Int, align: Layout.Alignment, spacingmult: Float, spacingadd: Float, includepad: Boolean)

DynamicLayout(base: CharSequence, display: CharSequence, paint: TextPaint, width: Int, align: Layout.Alignment, spacingmult: Float, spacingadd: Float, includepad: Boolean, ellipsize: TextUtils.TruncateAt?, ellipsizedWidth: Int)

Public methods
open Int

open Int

open Int

open Int

open LineBreakConfig

Gets the LineBreakConfig used in this DynamicLayout.

open Boolean

open Int

open Int

Layout.Directions!

open Int

open Int
getLineTop(line: Int)

open Int

open Int

Inherited functions
RectF computeDrawingBoundingBox()

Get an actual bounding box that draws text content. Note that the RectF.top and RectF.bottom may be different from the Layout.getLineTop(int) of the first line and Layout.getLineBottom(int) of the last line. The line top and line bottom are calculated based on yMin/yMax or ascent/descent value of font file. On the other hand, the drawing bounding boxes are calculated based on actual glyphs used there.

Unit draw(c: Canvas!)

Draw this Layout on the specified Canvas. This API draws background first, then draws text on top of it.

Unit draw(canvas: Canvas!, selectionHighlight: Path!, selectionHighlightPaint: Paint!, cursorOffsetVertical: Int)

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

Unit draw(canvas: Canvas, highlightPaths: MutableList<Path!>?, highlightPaints: MutableList<Paint!>?, selectionPath: Path?, selectionPaint: Paint?, cursorOffsetVertical: Int)

Draw this layout on the specified canvas. This API draws background first, then draws highlight paths on top of it, then draws selection or cursor, then finally draws text on top of it.

Unit drawBackground(canvas: Canvas)

Draw background of this layout. Different from draw(android.graphics.Canvas,java.util.List,java.util.List,android.graphics.Path,android.graphics.Paint,int) API, this API only draws background, not drawing text, highlights or selections. The background here is drawn by LineBackgroundSpan attached to the text.

Unit drawText(canvas: Canvas)

Draw text part of this layout. Different from draw(android.graphics.Canvas,java.util.List,java.util.List,android.graphics.Path,android.graphics.Paint,int) API, this API only draws text part, not drawing highlights, selections, or backgrounds.

Unit fillCharacterBounds(start: Int, end: Int, bounds: FloatArray, boundsStart: Int)

Return the characters' bounds in the given range. The bounds array will be filled starting from boundsStart (inclusive). The coordinates are in local text layout.

Layout.Alignment getAlignment()

Returns the alignment used for creating this layout in pixels.

Int getBreakStrategy()

Return the break strategy used for creating this layout.

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

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.

Float getDesiredWidth(source: CharSequence!, paint: TextPaint!)

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.

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.

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

TextUtils.TruncateAt? getEllipsize()

Return the ellipsize option used for creating this layout. May return null if no ellipsize option was selected.

Int getHeight()

Return the total height of this layout.

Int getHyphenationFrequency()

Return the hyphenation frequency used for creating this layout.

Int getJustificationMode()

Return the justification mode used for creating this layout.

IntArray? getLeftIndents()

Return a copy of the left indents used for this layout. May return null if no left indentation is applied.

Int getLineAscent(line: Int)

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

Int getLineBaseline(line: Int)

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

Int getLineBottom(line: Int)

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

Int getLineBottom(line: Int, includeLineSpacing: Boolean)

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

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.

Int getLineEnd(line: Int)

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

Int getLineForOffset(offset: 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.

Int getLineForVertical(vertical: 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.

Float getLineLeft(line: Int)

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

Int getLineLetterSpacingUnitCount(line: Int, includeTrailingWhitespace: Boolean)

Returns the number of letter spacing unit in the line.

This API returns a number of letters that is a target of letter spacing. The letter spacing won't be added to the middle of the characters that are needed to be treated as a single, e.g., ligatured or conjunct form. Note that this value is different from the number of] grapheme clusters that is calculated by BreakIterator.getCharacterInstance(Locale). For example, if the "fi" is ligatured, the ligatured form is treated as single uni and letter spacing is not added, but it has two separate grapheme cluster.

This value is used for calculating the letter spacing amount for the justification because the letter spacing is applied between clusters. For example, if extra W pixels needed to be filled by letter spacing, the amount of letter spacing to be applied is W/(letter spacing unit count - 1) px.

Float getLineMax(line: Int)

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

Float getLineRight(line: Int)

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

Float getLineSpacingAmount()

Returns the amount added to the line height.

Float getLineSpacingMultiplier()

Returns the multiplier applied to the line height.

Int getLineVisibleEnd(line: Int)

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

Float getLineWidth(line: Int)

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

Int getMaxLines()

Return the maximum lines allowed used for creating this layout. Note that this is not an actual line count of this layout. Use getLineCount() for getting the actual line count of this layout.

Paint.FontMetrics? getMinimumFontMetrics()

Get the minimum font metrics used for line spacing.

Int getOffsetForHorizontal(line: Int, horiz: Float)

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

Int getOffsetToLeftOf(offset: Int)

Int getOffsetToRightOf(offset: Int)

TextPaint getPaint()

Return the paint used for creating this layout. Do not modify the returned paint object. This paint object will still be used for drawing/measuring text.

Layout.Alignment! getParagraphAlignment(line: Int)

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

Int getParagraphLeft(line: Int)

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

Int getParagraphRight(line: Int)

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

Float getPrimaryHorizontal(offset: Int)

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.

IntArray? getRangeForRect(area: RectF, segmentFinder: SegmentFinder, inclusionStrategy: Layout.TextInclusionStrategy)

Finds the range of text which is inside the specified rectangle area. The start of the range is the start of the first text segment inside the area, and the end of the range is the end of the last text segment inside the area.

A text segment is considered to be inside the area according to the provided TextInclusionStrategy. If a text segment spans multiple lines or multiple directional runs (e.g. a hyphenated word), the text segment is divided into pieces at the line and run breaks, then the text segment is considered to be inside the area if any of its pieces are inside the area.

The returned range may also include text segments which are not inside the specified area, if those text segments are in between text segments which are inside the area. For example, the returned range may be "segment1 segment2 segment3" if "segment1" and "segment3" are inside the area and "segment2" is not.

IntArray? getRightIndents()

Return a copy of the right indents used for this layout. May return null if no right indentation is applied.

Float getSecondaryHorizontal(offset: Int)

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.

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

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.

Boolean getShiftDrawingOffsetForStartOverhang()

Returns true if shifting drawing offset for start overhang.

Float getSpacingAdd()

Returns the amount added to the line height.

Float getSpacingMultiplier()

Returns the multiplier applied to the line height.

CharSequence getText()

Return the text used for creating this layout.

TextDirectionHeuristic getTextDirectionHeuristic()

Returns the text direction heuristic used for creating this layout.

Boolean getUseBoundsForWidth()

Returns true if using bounding box as a width, false for using advance as a width.

Int getWidth()

Return the width used for creating this layout in pixels.

Unit increaseWidthTo(wid: Int)

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.

Boolean isFallbackLineSpacingEnabled()

Return true if the fallback line space is enabled in this Layout.

Boolean isFontPaddingIncluded()

Returns true if this layout is created with increased line height.

Boolean isRtlCharAt(offset: Int)

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

Boolean isSpanned()

Inherited properties
Layout.TextInclusionStrategy INCLUSION_STRATEGY_ANY_OVERLAP

Strategy which considers a text segment to be inside a rectangle area if the segment bounds intersect the rectangle.

Layout.TextInclusionStrategy INCLUSION_STRATEGY_CONTAINS_ALL

Strategy which considers a text segment to be inside a rectangle area if the segment bounds are completely contained within the rectangle.

Layout.TextInclusionStrategy INCLUSION_STRATEGY_CONTAINS_CENTER

Strategy which considers a text segment to be inside a rectangle area if the center of the segment bounds is inside the rectangle.

Public constructors

DynamicLayout

Added in API level 1
DynamicLayout(
    base: CharSequence,
    paint: TextPaint,
    width: Int,
    align: Layout.Alignment,
    spacingmult: Float,
    spacingadd: Float,
    includepad: Boolean)

Deprecated: Use Builder instead.

Parameters
base CharSequence: This value cannot be null.
paint TextPaint: This value cannot be null.
width Int: Value is 0 or greater
align Layout.Alignment: This value cannot be null.
spacingmult Float: Value is 0.0 or greater

DynamicLayout

Added in API level 1
DynamicLayout(
    base: CharSequence,
    display: CharSequence,
    paint: TextPaint,
    width: Int,
    align: Layout.Alignment,
    spacingmult: Float,
    spacingadd: Float,
    includepad: Boolean)

Deprecated: Use Builder instead.

Parameters
base CharSequence: This value cannot be null.
display CharSequence: This value cannot be null.
paint TextPaint: This value cannot be null.
width Int: Value is 0 or greater
align Layout.Alignment: This value cannot be null.
spacingmult Float: Value is 0.0 or greater

DynamicLayout

Added in API level 1
DynamicLayout(
    base: CharSequence,
    display: CharSequence,
    paint: TextPaint,
    width: Int,
    align: Layout.Alignment,
    spacingmult: Float,
    spacingadd: Float,
    includepad: Boolean,
    ellipsize: TextUtils.TruncateAt?,
    ellipsizedWidth: Int)

Deprecated: Use Builder instead.

Parameters
base CharSequence: This value cannot be null.
display CharSequence: This value cannot be null.
paint TextPaint: This value cannot be null.
width Int: Value is 0 or greater
align Layout.Alignment: This value cannot be null.
spacingmult Float: Value is 0.0 or greater
ellipsize TextUtils.TruncateAt?: This value may be null.
ellipsizedWidth Int: Value is 0 or greater

Public methods

getBottomPadding

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

getEllipsisCount

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

getEllipsisStart

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

getEllipsizedWidth

Added in API level 1
open fun getEllipsizedWidth(): Int
Return
Int the amount of ellipsized width in pixels. Value is 0 or greater

getLineBreakConfig

Added in API level 35
open fun getLineBreakConfig(): LineBreakConfig

Gets the LineBreakConfig used in this DynamicLayout. Use this only to consult the LineBreakConfig's properties and not to change them.

Return
LineBreakConfig The line break config in this DynamicLayout. This value cannot be null.

getLineContainsTab

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

getLineCount

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

getLineDescent

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

getLineDirections

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

getLineStart

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

getLineTop

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

getParagraphDirection

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

getTopPadding

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