Skip to content

Most visited

Recently visited

navigation

StaticLayout.Builder

public static final class StaticLayout.Builder
extends Object

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


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

Summary

Public methods

StaticLayout build()

Build the StaticLayout after options have been set.

static StaticLayout.Builder obtain(CharSequence source, int start, int end, TextPaint paint, int width)

Obtain a builder for constructing StaticLayout objects

StaticLayout.Builder setAlignment(Layout.Alignment alignment)

Set the alignment.

StaticLayout.Builder setBreakStrategy(int breakStrategy)

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

StaticLayout.Builder setEllipsize(TextUtils.TruncateAt ellipsize)

Set ellipsizing on the layout.

StaticLayout.Builder setEllipsizedWidth(int ellipsizedWidth)

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

StaticLayout.Builder setHyphenationFrequency(int hyphenationFrequency)

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

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

StaticLayout.Builder setIndents(int[] leftIndents, int[] rightIndents)

Set indents.

StaticLayout.Builder setJustificationMode(int justificationMode)

Set paragraph justification mode.

StaticLayout.Builder setLineSpacing(float spacingAdd, float spacingMult)

Set line spacing parameters.

StaticLayout.Builder setMaxLines(int maxLines)

Set maximum number of lines.

StaticLayout.Builder setText(CharSequence source)
StaticLayout.Builder setTextDirection(TextDirectionHeuristic textDir)

Set the text direction heuristic.

Protected methods

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Inherited methods

From class java.lang.Object

Public methods

build

added in API level 23
StaticLayout build ()

Build the StaticLayout 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
StaticLayout the newly constructed StaticLayout object

obtain

added in API level 23
StaticLayout.Builder obtain (CharSequence source, 
                int start, 
                int end, 
                TextPaint paint, 
                int width)

Obtain a builder for constructing StaticLayout objects

Parameters
source CharSequence: The text to be laid out, optionally with spans

start int: The index of the start of the text

end int: The index + 1 of the end of the text

paint TextPaint: The base paint used for layout

width int: The width in pixels

Returns
StaticLayout.Builder a builder object used for constructing the StaticLayout

setAlignment

added in API level 23
StaticLayout.Builder setAlignment (Layout.Alignment alignment)

Set the alignment. The default is ALIGN_NORMAL.

Parameters
alignment Layout.Alignment: Alignment for the resulting StaticLayout

Returns
StaticLayout.Builder this builder, useful for chaining

setBreakStrategy

added in API level 23
StaticLayout.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

Returns
StaticLayout.Builder this builder, useful for chaining

setEllipsize

added in API level 23
StaticLayout.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
StaticLayout.Builder this builder, useful for chaining

setEllipsizedWidth

added in API level 23
StaticLayout.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, int, int, TextPaint, int).

Parameters
ellipsizedWidth int: width used for ellipsizing, in pixels

Returns
StaticLayout.Builder this builder, useful for chaining

setHyphenationFrequency

added in API level 23
StaticLayout.Builder setHyphenationFrequency (int hyphenationFrequency)

Set hyphenation frequency, to control the amount of automatic hyphenation used. The default is HYPHENATION_FREQUENCY_NONE.

Parameters
hyphenationFrequency int: hyphenation frequency for the paragraph

Returns
StaticLayout.Builder this builder, useful for chaining

setIncludePad

added in API level 23
StaticLayout.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
StaticLayout.Builder this builder, useful for chaining

setIndents

added in API level 23
StaticLayout.Builder setIndents (int[] leftIndents, 
                int[] rightIndents)

Set indents. Arguments are arrays holding an indent amount, one per line, measured in pixels. For lines past the last element in the array, the last element repeats.

Parameters
leftIndents int: array of indent values for left margin, in pixels

rightIndents int: array of indent values for right margin, in pixels

Returns
StaticLayout.Builder this builder, useful for chaining

setJustificationMode

added in API level 26
StaticLayout.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.

Returns
StaticLayout.Builder this builder, useful for chaining.

setLineSpacing

added in API level 23
StaticLayout.Builder setLineSpacing (float spacingAdd, 
                float spacingMult)

Set line spacing parameters. The default is 0.0 for spacingAdd and 1.0 for spacingMult.

Parameters
spacingAdd float: line spacing add

spacingMult float: line spacing multiplier

Returns
StaticLayout.Builder this builder, useful for chaining

setMaxLines

added in API level 23
StaticLayout.Builder setMaxLines (int maxLines)

Set maximum number of lines. This is particularly useful in the case of ellipsizing, where it changes the layout of the last line. The default is unlimited.

Parameters
maxLines int: maximum number of lines in the layout

Returns
StaticLayout.Builder this builder, useful for chaining

See also:

setText

added in API level 23
StaticLayout.Builder setText (CharSequence source)

Parameters
source CharSequence

Returns
StaticLayout.Builder

setTextDirection

added in API level 23
StaticLayout.Builder setTextDirection (TextDirectionHeuristic textDir)

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

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

Returns
StaticLayout.Builder this builder, useful for chaining

Protected methods

finalize

added in API level 23
void finalize ()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the Java™ virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Throws
Throwable
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)