Class ExpandableText

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class ExpandableText extends Container

Long text shown as its first few lines, ending in "...", with a "Read more" link that expands it in place and a "Show less" link that collapses it again.

A collapsed paragraph with a More link

Form form = new Form("About", BoxLayout.y());
String longDescription = "A long paragraph of product details that runs well past "
        + "three lines on a phone screen, so only its start shows until the user "
        + "taps the link to read the rest of it.";
ExpandableText about = new ExpandableText(longDescription, 3);
form.add(about);
form.show();

The link appears only when the text needs more lines than the limit at the width it was given, so the same component can hold a short description without offering to expand it. Change the link texts with #setLinkText(String, String); they go through the localization bundle.

UIIDs: ExpandableText for the container, ExpandableTextBody for the text and ExpandableTextLink for the link, falling back to Label and a borderless Button look.

  • Constructor Details

    • ExpandableText

      public ExpandableText(String text)

      Text collapsed to three lines.

      Parameters
      • text: the text
    • ExpandableText

      public ExpandableText(String text, int maxLines)

      Text collapsed to maxLines lines.

      Parameters
      • text: the text

      • maxLines: how many lines show while collapsed, at least 1

  • Method Details

    • initUnselectedStyle

      protected void initUnselectedStyle(Style unselectedStyle)
      Description copied from class: Component

      Can be overridden by subclasses to perform initialization when the unselected style is set to a new value.

      Parameters
      • unselectedStyle: The unselected style.
      Overrides:
      initUnselectedStyle in class Component
    • getText

      public String getText()

      The text.

      Returns

      the full text

    • setText

      public final void setText(String text)

      Sets the text.

      Parameters
      • text: the full text
    • getMaxLines

      public int getMaxLines()

      How many lines show while collapsed.

      Returns

      the line limit

    • setMaxLines

      public final void setMaxLines(int maxLines)

      Sets how many lines show while collapsed.

      Parameters
      • maxLines: at least 1
    • isExpanded

      public boolean isExpanded()

      True while the whole text shows.

      Returns

      whether the text is expanded

    • setExpanded

      public void setExpanded(boolean expanded)

      Expands or collapses the text.

      Parameters
      • expanded: true for the whole text
    • setLinkText

      public void setLinkText(String more, String less)

      Sets the texts of the link.

      Parameters
      • more: shown while collapsed, "Read more" by default

      • less: shown while expanded, "Show less" by default

    • getLink

      public Button getLink()

      The link that expands and collapses the text.

      Returns

      the link button

    • getBody

      public SpanLabel getBody()

      The label holding the text.

      Returns

      the text

    • isTruncatable

      public boolean isTruncatable()

      True when the text needs more lines than the limit at the current width.

      Returns

      whether there is anything to expand

    • layoutContainer

      public void layoutContainer()
      Description copied from class: Container
      Performs the layout of the container if a layout is necessary
      Overrides:
      layoutContainer in class Container