DynamicLayout.Builder

public static final class DynamicLayout.Builder
extends Object

java.lang.Object
   ↳ android.text.DynamicLayout.Builder


Builder for dynamic layouts. The builder is the preferred pattern for constructing DynamicLayout objects and should be preferred over the constructors, particularly to access newer features. To build a dynamic layout, first call obtain(CharSequence, TextPaint, int) with the required arguments (base, paint, and width), then call setters for optional parameters, and finally build() to build the DynamicLayout object. Parameters not explicitly set will get default values.

Summary

Public methods

DynamicLayout build()

Build the DynamicLayout after options have been set.

static DynamicLayout.Builder obtain(CharSequence base, TextPaint paint, int width)

Obtain a builder for constructing DynamicLayout objects.

DynamicLayout.Builder setAlignment(Layout.Alignment alignment)

Set the alignment.

DynamicLayout.Builder setBreakStrategy(int breakStrategy)

Set break strategy, useful for selecting high quality or balanced paragraph layout options.

DynamicLayout.Builder setDisplayText(CharSequence display)

Set the transformed text (password transformation being the primary example of a transformation) that will be updated as the base text is changed.

DynamicLayout.Builder setEllipsize(TextUtils.TruncateAt ellipsize)

Set ellipsizing on the layout.

DynamicLayout.Builder setEllipsizedWidth(int ellipsizedWidth)

Set the width as used for ellipsizing purposes, if it differs from the normal layout width.

DynamicLayout.Builder setHyphenationFrequency(int hyphenationFrequency)

Set hyphenation frequency, to control the amount of automatic hyphenation used.

DynamicLayout.Builder setIncludePad(boolean includePad)

Set whether to include extra space beyond font ascent and descent (which is needed to avoid clipping in some languages, such as Arabic and Kannada).

DynamicLayout.Builder setJustificationMode(int justificationMode)

Set paragraph justification mode.

DynamicLayout.Builder setLineSpacing(float spacingAdd, float spacingMult)

Set line spacing parameters.

DynamicLayout.Builder setTextDirection(TextDirectionHeuristic textDir)

Set the text direction heuristic.

DynamicLayout.Builder setUseLineSpacingFromFallbacks(boolean useLineSpacingFromFallbacks)

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).

Inherited methods

Public methods

build

DynamicLayout build ()

Build the DynamicLayout after options have been set.

Note: the builder object must not be reused in any way after calling this method. Setting parameters after calling this method, or calling it a second time on the same builder object, will likely lead to unexpected results.

Returns
DynamicLayout the newly constructed DynamicLayout object

This value will never be null.

obtain

DynamicLayout.Builder obtain (CharSequence base, 
                TextPaint paint, 
                int width)

Obtain a builder for constructing DynamicLayout objects.

Parameters
base CharSequence

This value must never be null.

paint TextPaint

This value must never be null.

width int

Returns
DynamicLayout.Builder

This value will never be null.

setAlignment

DynamicLayout.Builder setAlignment (Layout.Alignment alignment)

Set the alignment. The default is ALIGN_NORMAL.

Parameters
alignment Layout.Alignment: Alignment for the resulting DynamicLayout

This value must never be null.

Returns
DynamicLayout.Builder this builder, useful for chaining

This value will never be null.

setBreakStrategy

DynamicLayout.Builder setBreakStrategy (int breakStrategy)

Set break strategy, useful for selecting high quality or balanced paragraph layout options. The default is BREAK_STRATEGY_SIMPLE.

Parameters
breakStrategy int: break strategy for paragraph layout

Value is BREAK_STRATEGY_SIMPLE, BREAK_STRATEGY_HIGH_QUALITY or BREAK_STRATEGY_BALANCED.

Returns
DynamicLayout.Builder this builder, useful for chaining

This value will never be null.

setDisplayText

DynamicLayout.Builder setDisplayText (CharSequence display)

Set the transformed text (password transformation being the primary example of a transformation) that will be updated as the base text is changed. The default is the 'base' text passed to the builder's constructor.

Parameters
display CharSequence: the transformed text

This value must never be null.

Returns
DynamicLayout.Builder this builder, useful for chaining

This value will never be null.

setEllipsize

DynamicLayout.Builder setEllipsize (TextUtils.TruncateAt ellipsize)

Set ellipsizing on the layout. Causes words that are longer than the view is wide, or exceeding the number of lines (see #setMaxLines) in the case of END or MARQUEE, to be ellipsized instead of broken. The default is null, indicating no ellipsis is to be applied.

Parameters
ellipsize TextUtils.TruncateAt: type of ellipsis behavior

This value may be null.

Returns
DynamicLayout.Builder this builder, useful for chaining

setEllipsizedWidth

DynamicLayout.Builder setEllipsizedWidth (int ellipsizedWidth)

Set the width as used for ellipsizing purposes, if it differs from the normal layout width. The default is the width passed to obtain(CharSequence, TextPaint, int).

Parameters
ellipsizedWidth int: width used for ellipsizing, in pixels

Returns
DynamicLayout.Builder this builder, useful for chaining

This value will never be null.

setHyphenationFrequency

DynamicLayout.Builder setHyphenationFrequency (int hyphenationFrequency)

Set hyphenation frequency, to control the amount of automatic hyphenation used. The possible values are defined in Layout, by constants named with the pattern HYPHENATION_FREQUENCY_*. The default is HYPHENATION_FREQUENCY_NONE.

Parameters
hyphenationFrequency int: hyphenation frequency for the paragraph

Value is HYPHENATION_FREQUENCY_NORMAL, HYPHENATION_FREQUENCY_FULL or HYPHENATION_FREQUENCY_NONE.

Returns
DynamicLayout.Builder this builder, useful for chaining

This value will never be null.

setIncludePad

DynamicLayout.Builder setIncludePad (boolean includePad)

Set whether to include extra space beyond font ascent and descent (which is needed to avoid clipping in some languages, such as Arabic and Kannada). The default is true.

Parameters
includePad boolean: whether to include padding

Returns
DynamicLayout.Builder this builder, useful for chaining

This value will never be null.

setJustificationMode

DynamicLayout.Builder setJustificationMode (int justificationMode)

Set paragraph justification mode. The default value is JUSTIFICATION_MODE_NONE. If the last line is too short for justification, the last line will be displayed with the alignment set by setAlignment(Layout.Alignment).

Parameters
justificationMode int: justification mode for the paragraph.

Value is JUSTIFICATION_MODE_NONE or JUSTIFICATION_MODE_INTER_WORD.

Returns
DynamicLayout.Builder this builder, useful for chaining.

This value will never be null.

setLineSpacing

DynamicLayout.Builder setLineSpacing (float spacingAdd, 
                float spacingMult)

Set line spacing parameters. Each line will have its line spacing multiplied by spacingMult and then increased by spacingAdd. The default is 0.0 for spacingAdd and 1.0 for spacingMult.

Parameters
spacingAdd float: the amount of line spacing addition

spacingMult float: the line spacing multiplier

Value is 0.0 or greater.

Returns
DynamicLayout.Builder this builder, useful for chaining

This value will never be null.

setTextDirection

DynamicLayout.Builder setTextDirection (TextDirectionHeuristic textDir)

Set the text direction heuristic. The text direction heuristic is used to resolve text direction per-paragraph based on the input text. The default is FIRSTSTRONG_LTR.

Parameters
textDir TextDirectionHeuristic: text direction heuristic for resolving bidi behavior.

This value must never be null.

Returns
DynamicLayout.Builder this builder, useful for chaining

This value will never be null.

setUseLineSpacingFromFallbacks

DynamicLayout.Builder setUseLineSpacingFromFallbacks (boolean useLineSpacingFromFallbacks)

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.

For backward compatibility reasons, the default is false, but setting this to true is strongly recommended. It is required to be true if text could be in languages like Burmese or Tibetan where text is typically much taller or deeper than Latin text.

Parameters
useLineSpacingFromFallbacks boolean: whether to expand linespacing based on fallback fonts

Returns
DynamicLayout.Builder this builder, useful for chaining

This value will never be null.