Skip to content

Toggle

Since 2.2.1

Toggle is a checkable UI component — a square button with a mark icon and an optional label. Clicking toggles its on/off state. Multiple toggles can be linked into a ToggleGroup for exclusive (radio-button-like) selection.

Internally, Toggle is a horizontal flex row containing a Button (the clickable box with the mark icon inside) and a Label (the text label). Both are internal children.

INFO

Everything documented on UIElement (layout, styles, events, data bindings, etc.) applies here too.


Usage

java
var toggle = new Toggle();
toggle.setText("my.toggle.label", true); // translated
toggle.setOn(true);
toggle.setOnToggleChanged(isOn -> {
    // called whenever the state changes
});
parent.addChild(toggle);

XML

xml
<!-- Simple toggle with a translated label -->
<toggle text="my.toggle.label"/>

<!-- Toggle in the on state -->
<toggle text="my.toggle.label" is-on="true"/>

<!-- Empty text attribute hides the label (calls noText()) -->
<toggle text=""/>

<!-- Style internals -->
<toggle text="my.toggle.label">
    <internal index="0">
        <!-- Button (the square box) attributes / children -->
    </internal>
    <internal index="1">
        <!-- Label attributes -->
    </internal>
</toggle>
XML AttributeTypeDescription
textstringSets the label text. Pass an empty string to hide the label.
is-onbooleanInitial on/off state. Default: false.

Internal Structure

Toggle contains two internal elements:

IndexFieldTypeCSS classDescription
0toggleButtonButton.__toggle_button__The clickable square box (contains the mark icon).
1toggleLabelLabel.__toggle_label__The text label to the right of the box.

The mark icon lives inside toggleButton and can be targeted via CSS:

css
/* Style the mark / unmark icon */
.__toggle_mark-icon__ {
    /* override size, padding, etc. */
}

/* Target the label specifically */
toggle > label.__toggle_label__ {
    font-size: 9;
    text-color: #CCCCCC;
}

Toggle Style

ToggleStyle controls both the box button textures and the mark/unmark icon.

INFO

base-background / hover-background

Textures for the inner Button in idle and hovered states (delegated to ButtonStyle).

Default: Sprites.RECT_DARK / Sprites.RECT_DARK + white border

java
toggle.toggleStyle(style -> {
    style.baseTexture(myIdleTexture);   // sets both base & pressed
    style.hoverTexture(myHoverTexture);
});

INFO

unmark-background

The icon displayed inside the box when the toggle is off.

Default: none (transparent)

java
toggle.toggleStyle(style -> style.unmarkTexture(myUncheckedIcon));

INFO

mark-background

The icon displayed inside the box when the toggle is on.

Default: Icons.CHECK_SPRITE

java
toggle.toggleStyle(style -> style.markTexture(myCheckedIcon));

Toggle Group

A ToggleGroup links multiple Toggle instances so that only one can be active at a time (like a radio button group). When allowEmpty is false (default), at least one toggle is always active.

java
var group = new Toggle.ToggleGroup();
// group.setAllowEmpty(true); // allow all toggles to be off

var t1 = new Toggle().setText("Option A", true).setToggleGroup(group);
var t2 = new Toggle().setText("Option B", true).setToggleGroup(group);
var t3 = new Toggle().setText("Option C", true).setToggleGroup(group);

group.getCurrentToggle(); // the currently active Toggle (or null if allowEmpty)

TIP

Use ToggleGroupElement to manage the group automatically through the XML / editor — it registers children as they are added and unregisters them when removed.


Text

setText / noText / enableText

Controls the label displayed to the right of the toggle box.

java
toggle.setText("my.translation.key", true);  // translated
toggle.setText("Literal label", false);        // literal
toggle.noText();                               // hide the label
toggle.enableText();                           // show it again

Value Binding

Toggle extends BindableUIElement&lt;Boolean&gt;, so it integrates with the data-binding system:

java
toggle.bind(DataBindingBuilder.bool(
    () -> myState.isEnabled(),
    val -> myState.setEnabled(val)
).build());

See Data Bindings for full details.


Fields

NameTypeAccessDescription
toggleButtonButtonpublic finalThe inner clickable box.
markIconUIElementpublic finalThe icon element inside the box.
toggleLabelLabelpublic finalThe label element to the right.
toggleStyleToggleStyleprivate (getter)Current toggle style.
isOnbooleanprivate (getter)Current on/off state.
toggleGroupToggleGroupprivate (getter/nullable)The group this toggle belongs to.

Methods

MethodReturnsDescription
setOn(boolean)ToggleSets the on/off state and notifies listeners.
setToggleGroup(ToggleGroup)ToggleJoins a ToggleGroup. Pass null to leave the group.
setOnToggleChanged(BooleanConsumer)ToggleRegisters a listener for state changes.
setText(String, boolean)ToggleSets label text. true = translatable.
noText()ToggleHides the label.
enableText()ToggleShows the label.
toggleStyle(Consumer&lt;ToggleStyle&gt;)ToggleConfigures ToggleStyle fluently.
toggleButton(Consumer&lt;Button&gt;)ToggleConfigures the inner Button directly.
toggleLabel(Consumer&lt;Label&gt;)ToggleConfigures the label directly.
markIcon(Consumer&lt;UIElement&gt;)ToggleConfigures the mark icon element directly.
getValue()BooleanReturns the current on/off state.

Released under the MIT License.