Class Skeleton

All Implemented Interfaces:
Animation, Editable, StyleListener

public class Skeleton extends CommonProgressAnimations.ProgressAnimation

A skeleton screen placeholder (also called a shimmer loader, content placeholder or ghost element): the grey outline of content that is still loading, with a light band sweeping across it.

Show one where content will land, so the screen says "loading" without a spinner and does not jump when the content arrives:

Form form = new Form("Orders", new BorderLayout());
Container list = new Container(BoxLayout.y());
list.add(Skeleton.rows(4));
form.add(BorderLayout.CENTER, list);
form.show();
// Stands in for a network call that answers after 2 seconds.
UITimer.timer(2000, false, form, () -> {
    list.removeAll();
    for (String order : new String[] {"Order #101", "Order #102"}) {
        list.add(new MultiButton(order));
    }
    list.animateLayout(200);
});

Or mask a component that already exists until its data is ready:

Form form = new Form("Profile", BoxLayout.y());
SpanLabel bio = new SpanLabel();
form.add(bio);
form.show();
Skeleton.show(bio, Skeleton.text(3));
UITimer.timer(2000, false, form, () -> {
    bio.setText("Builds mobile apps in Java.");
    Skeleton.hide(bio, CommonTransitions.createFade(200));
});

The shapes are drawn in the foreground colour of the Skeleton UIID. A theme that does not style that UIID gets the theme's text colour at a low opacity, which suits light and dark themes alike. Restyle the UIID, or pass your own with setUIID, to change the colour, padding or margin. The shimmer runs only while the placeholder is on screen; turn it off with #shimmer(boolean).

Skeleton is a CommonProgressAnimations.ProgressAnimation, so it also works with CommonProgressAnimations.ProgressAnimation#markComponentLoading.

See also InfiniteProgress for a blocking spinner and EmptyState for what to show when loading finished with nothing to show.

  • Constructor Details

    • Skeleton

      public Skeleton()
      A placeholder block that fills whatever size it is given. This is the shape #show(Component) uses to mask a component.
  • 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
    • rows

      public static Skeleton rows(int rows)

      The outline of a list: per row, a round icon, a title, a shorter second line and a value at the far end.

      Parameters
      • rows: how many rows to outline
      Returns

      the placeholder

    • text

      public static Skeleton text(int lines)

      Lines of text, the last one shorter, as a paragraph would end.

      Parameters
      • lines: how many lines
      Returns

      the placeholder

    • circle

      public static Skeleton circle(float diameterMM)

      A circle, for an avatar or an icon.

      Parameters
      • diameterMM: the diameter in millimetres
      Returns

      the placeholder

    • rect

      public static Skeleton rect(float widthMM, float heightMM)

      A rounded rectangle, for an image, a button or a chart.

      Parameters
      • widthMM: the width in millimetres; 0 or less takes the full width

      • heightMM: the height in millimetres

      Returns

      the placeholder

    • card

      public static Skeleton card()

      A card: an image across the top, then a title and two lines of text.

      Returns

      the placeholder

    • shimmer

      public Skeleton shimmer(boolean shimmer)

      Turns the moving shimmer band on or off. It is on by default; off, the placeholder is a still outline.

      Parameters
      • shimmer: true for the moving band
      Returns

      this, for chaining

    • uiid

      public Skeleton uiid(String uiid)

      Sets the UIID, for chaining: Skeleton.rows(5).uiid("MySkeleton").

      Parameters
      • uiid: the style whose foreground the shapes are drawn in
      Returns

      this, for chaining

    • isShimmer

      public boolean isShimmer()

      Whether the shimmer band is on.

      Returns

      true if the band moves across the placeholder

    • show

      public static Skeleton show(Component cmp)

      Replaces a component in its parent with a block placeholder of the same preferred size until #hide(Component) is called. Calling it again while the component is masked returns the placeholder already showing.

      Parameters
      • cmp: a component that has a parent
      Returns

      the placeholder that took its place

    • show

      public static Skeleton show(Component cmp, Skeleton placeholder)

      Replaces a component in its parent with the given placeholder until #hide(Component) is called, for instance Skeleton.text(3) in place of a SpanLabel.

      Parameters
      • cmp: a component that has a parent

      • placeholder: what to show in its place

      Returns

      the placeholder showing; when the component was already masked by a Skeleton, that one; null when another kind of progress animation already masks it

    • hide

      public static void hide(Component cmp)

      Puts a component masked by #show(Component) back in its place. Does nothing if it is not masked.

      Parameters
      • cmp: the component to restore
    • hide

      public static void hide(Component cmp, Transition t)

      Puts a component masked by #show(Component) back in its place with a transition, such as a fade. Does nothing if it is not masked.

      Parameters
      • cmp: the component to restore

      • t: the transition, or null to swap at once

    • initComponent

      protected void initComponent()
      Description copied from class: Component
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class CommonProgressAnimations.ProgressAnimation
    • animate

      public boolean animate()
      Description copied from class: Component

      Allows the animation to reduce "repaint" calls when it returns false. It is called once for every frame. Frames are defined by the com.codename1.ui.Display class.

      Returns

      true if a repaint is desired or false if no repaint is necessary

      Specified by:
      animate in interface Animation
      Overrides:
      animate in class Component
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Component
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Component