Added in API level 35

Builder


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

A builder class for Layout object. Different from StaticLayout.Builder, this builder generates the optimal layout based on input. If the given text and parameters can be rendered with BoringLayout, this builder generates BoringLayout instance. Otherwise, StaticLayout instance is generated.

Summary

Public constructors
Builder(text: CharSequence, start: Int, end: Int, paint: TextPaint, width: Int)

Construct a builder class.

Public methods
Layout

Build a Layout object.

Layout.Builder

Set the text alignment.

Layout.Builder
setBreakStrategy(breakStrategy: Int)

Set the line break strategy.

Layout.Builder

Set the ellipsizing type.

Layout.Builder
setEllipsizedWidth(ellipsizeWidth: Int)

Set the width as used for ellipsizing purpose in pixels.

Layout.Builder
setFallbackLineSpacingEnabled(fallbackLineSpacing: Boolean)

Set whether to respect the ascent and descent of the fallback fonts.

Layout.Builder
setFontPaddingIncluded(includeFontPadding: Boolean)

Set whether including extra padding into the first and the last line height.

Layout.Builder
setHyphenationFrequency(hyphenationFrequency: Int)

Set the hyphenation frequency.

Layout.Builder
setJustificationMode(justificationMode: Int)

Set justification mode.

Layout.Builder
setLeftIndents(leftIndents: IntArray?)

Set visually left indents in pixels per lines.

Layout.Builder

Set the line break configuration.

Layout.Builder

Set the line spacing amount.

Layout.Builder

Set the line spacing multiplier.

Layout.Builder
setMaxLines(maxLines: Int)

Set the maximum number of lines.

Layout.Builder

Set the minimum font metrics used for line spacing.

Layout.Builder
setRightIndents(rightIndents: IntArray?)

Set visually right indents in pixels per lines.

Layout.Builder
setShiftDrawingOffsetForStartOverhang(shiftDrawingOffsetForStartOverhang: Boolean)

Set true for shifting the drawing x offset for showing overhang at the start position.

Layout.Builder

Set the text direction heuristics.

Layout.Builder
setUseBoundsForWidth(useBoundsForWidth: Boolean)

Set true for using width of bounding box as a source of automatic line breaking and drawing.

Public constructors

Builder

Added in API level 35
Builder(
    text: CharSequence,
    start: Int,
    end: Int,
    paint: TextPaint,
    width: Int)

Construct a builder class.

Parameters
text CharSequence: a text to be displayed. This value cannot be null.
start Int: an inclusive start index of the text to be displayed. Value is 0 or greater
end Int: an exclusive end index of the text to be displayed. Value is 0 or greater
paint TextPaint: a paint object to be used for drawing text. This value cannot be null.
width Int: a width constraint in pixels. Value is 0 or greater

Public methods

build

Added in API level 35
fun build(): Layout

Build a Layout object.

Return
Layout This value cannot be null.

setAlignment

Added in API level 35
fun setAlignment(alignment: Layout.Alignment): Layout.Builder

Set the text alignment. The default value is Layout.Alignment.ALIGN_NORMAL.

Parameters
alignment Layout.Alignment: an alignment. This value cannot be null.
Return
Layout.Builder this builder instance. This value cannot be null.

setEllipsize

Added in API level 35
fun setEllipsize(ellipsize: TextUtils.TruncateAt?): Layout.Builder

Set the ellipsizing type. By setting null, the ellipsize is disabled. The default value is null.

Parameters
ellipsize TextUtils.TruncateAt?: type of the ellipsize. null for disabling ellipsize.
Return
Layout.Builder this builder instance.

setEllipsizedWidth

Added in API level 35
fun setEllipsizedWidth(ellipsizeWidth: Int): Layout.Builder

Set the width as used for ellipsizing purpose in pixels. The passed value is ignored and forced to set to the value of width constraint passed in constructor if no ellipsize option is set. The default value is the width constraint.

Parameters
ellipsizeWidth Int: a ellipsizing width in pixels. Value is 0 or greater
Return
Layout.Builder this builder instance. This value cannot be null.

setFallbackLineSpacingEnabled

Added in API level 35
fun setFallbackLineSpacingEnabled(fallbackLineSpacing: Boolean): Layout.Builder

Set whether to respect the ascent and descent of the fallback fonts. Set whether to respect the ascent and descent of the fallback fonts that are used in displaying the text (which is needed to avoid text from consecutive lines running into each other). If set, fallback fonts that end up getting used can increase the ascent and descent of the lines that they are used on. The default value is false

Parameters
fallbackLineSpacing Boolean: whether to expand line height based on fallback fonts.
Return
Layout.Builder this builder instance. This value cannot be null.

setFontPaddingIncluded

Added in API level 35
fun setFontPaddingIncluded(includeFontPadding: Boolean): Layout.Builder

Set whether including extra padding into the first and the last line height. By setting true, the first line of the text and the last line of the text will have extra vertical space for avoiding clipping. The default value is true.

Parameters
includeFontPadding Boolean: true for including extra space into first and last line.
Return
Layout.Builder this builder instance. This value cannot be null.

setJustificationMode

Added in API level 35
fun setJustificationMode(justificationMode: Int): Layout.Builder

Set justification mode. When justification mode is Layout.JUSTIFICATION_MODE_INTER_WORD, the word spacing on the given Paint passed to the constructor will be ignored. This behavior also affects spans which change the word spacing. The default value is Layout.JUSTIFICATION_MODE_NONE.

Parameters
justificationMode Int: justification mode. Value is android.graphics.text.LineBreaker#JUSTIFICATION_MODE_NONE, android.graphics.text.LineBreaker#JUSTIFICATION_MODE_INTER_WORD, or android.graphics.text.LineBreaker#JUSTIFICATION_MODE_INTER_CHARACTER
Return
Layout.Builder this builder instance. This value cannot be null.

setLeftIndents

Added in API level 35
fun setLeftIndents(leftIndents: IntArray?): Layout.Builder

Set visually left indents in pixels per lines. For the lines past the last element in the array, the last element repeats. Passing null for disabling indents. Note that even with the RTL layout, this method reserve spacing at the visually left of the line. The default value is null.

Parameters
leftIndents IntArray?: array of indents values for the left margins in pixels. This value may be null.
Return
Layout.Builder this builder instance.

setLineBreakConfig

Added in API level 35
fun setLineBreakConfig(lineBreakConfig: LineBreakConfig): Layout.Builder

Set the line break configuration. The default value is a LinebreakConfig instance that has LineBreakConfig.LINE_BREAK_STYLE_NONE and LineBreakConfig.LINE_BREAK_WORD_STYLE_NONE.

Parameters
lineBreakConfig LineBreakConfig: the line break configuration This value cannot be null.
Return
Layout.Builder this builder instance. This value cannot be null.

setLineSpacingAmount

Added in API level 35
fun setLineSpacingAmount(amount: Float): Layout.Builder

Set the line spacing amount. The specified amount of pixels will be added to each line. The default value is 0. The negative value is allowed for squeezing lines.

Parameters
amount Float: an amount of pixels to be added to line height.
Return
Layout.Builder this builder instance. This value cannot be null.

setLineSpacingMultiplier

Added in API level 35
fun setLineSpacingMultiplier(multiplier: Float): Layout.Builder

Set the line spacing multiplier. The specified value will be multiplied to each line. The default value is 1.

Parameters
multiplier Float: a multiplier to be applied to the line height Value is 0 or greater
Return
Layout.Builder this builder instance. This value cannot be null.

setMaxLines

Added in API level 35
fun setMaxLines(maxLines: Int): Layout.Builder

Set the maximum number of lines. The default value is unlimited.

Parameters
maxLines Int: maximum number of lines in the layout. Value is 1 or greater
Return
Layout.Builder this builder instance. This value cannot be null.

setMinimumFontMetrics

Added in API level 35
fun setMinimumFontMetrics(minimumFontMetrics: Paint.FontMetrics?): Layout.Builder

Set the minimum font metrics used for line spacing.

null is the default value. If null is set or left it as default, the font metrics obtained by Paint.getFontMetricsForLocale(Paint.FontMetrics) is used.

The minimum meaning here is the minimum value of line spacing: maximum value of Paint.ascent(), minimum value of Paint.descent().

By setting this value, each line will have minimum line spacing regardless of the text rendered. For example, usually Japanese script has larger vertical metrics than Latin script. By setting the metrics obtained by Paint.getFontMetricsForLocale(Paint.FontMetrics) for Japanese or leave it null if the Paint's locale is Japanese, the line spacing for Japanese is reserved if the text is an English text. If the vertical metrics of the text is larger than Japanese, for example Burmese, the bigger font metrics is used.

Parameters
minimumFontMetrics Paint.FontMetrics?: A minimum font metrics. Passing null for using the value obtained by Paint.getFontMetricsForLocale(Paint.FontMetrics)

setRightIndents

Added in API level 35
fun setRightIndents(rightIndents: IntArray?): Layout.Builder

Set visually right indents in pixels per lines. For the lines past the last element in the array, the last element repeats. Passing null for disabling indents. Note that even with the RTL layout, this method reserve spacing at the visually right of the line. The default value is null.

Parameters
rightIndents IntArray?: array of indents values for the right margins in pixels. This value may be null.
Return
Layout.Builder this builder instance.

setShiftDrawingOffsetForStartOverhang

Added in API level 35
fun setShiftDrawingOffsetForStartOverhang(shiftDrawingOffsetForStartOverhang: Boolean): Layout.Builder

Set true for shifting the drawing x offset for showing overhang at the start position. This flag is ignored if the getUseBoundsForWidth() is false. If this value is false, the Layout draws text from the zero even if there is a glyph stroke in a region where the x coordinate is negative. If this value is true, the Layout draws text with shifting the x coordinate of the drawing bounding box. This value is false by default.

Parameters
shiftDrawingOffsetForStartOverhang Boolean: true for shifting the drawing offset for showing the stroke that is in the region where the x coordinate is negative.
Return
Layout.Builder This value cannot be null.

setTextDirectionHeuristic

Added in API level 35
fun setTextDirectionHeuristic(textDirection: TextDirectionHeuristic): Layout.Builder

Set the text direction heuristics. The text direction heuristics is used to resolve text direction on the text. The default value is TextDirectionHeuristics.FIRSTSTRONG_LTR

Parameters
textDirection TextDirectionHeuristic: a text direction heuristic. This value cannot be null.
Return
Layout.Builder this builder instance. This value cannot be null.

setUseBoundsForWidth

Added in API level 35
fun setUseBoundsForWidth(useBoundsForWidth: Boolean): Layout.Builder

Set true for using width of bounding box as a source of automatic line breaking and drawing. If this value is false, the Layout determines the drawing offset and automatic line breaking based on total advances. By setting true, use all joined glyph's bounding boxes as a source of text width. If the font has glyphs that have negative bearing X or its xMax is greater than advance, the glyph clipping can happen because the drawing area may be bigger. By setting this to true, the Layout will reserve more spaces for drawing.

Parameters
useBoundsForWidth Boolean: True for using bounding box, false for advances.
Return
Layout.Builder this builder instance This value cannot be null.