> For the complete documentation index, see [llms.txt](https://learn.totalcross.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.totalcross.com/master/documentation/components/switch.md).

# Switch

### Overview

Switch is a element that chooses between two proportions

{% code title="SwitchSample.java" %}

```java
 // Instance a simple switch
subtitles = new Switch();

 // Set the colors up
subtitles.colorBallOn = Color.getRGB(0,150,136);
subtitles.colorBarOn = Colors.P_700;
subtitles.colorBallOn = Color.getRGB(241,241,241);
subtitles.colorBarOff = Colors.P_200;

 // Positions the switch 
add(subtitles, RIGHT-hGap, SAME+lsfx.getHeight()/2, PREFERRED, PREFERRED);
```

{% endcode %}

{% hint style="info" %}
This sample code is only from the Switch, to see the complete sample, including the Slider, go to [github](https://github.com/TotalCross/TCSample/blob/master/src/main/java/totalcross/sample/components/ui/SliderSwitchSample.java)
{% endhint %}

### Attributes

| Type        | Name         | Description                                                     |
| ----------- | ------------ | --------------------------------------------------------------- |
| **boolean** | centerText   | Boolean that center the text instead of move from left to right |
| **int**     | colorBackOff | Background color for off switch                                 |
| **int**     | colorBackOn  | Background color for on switch                                  |
| **int**     | colorForeOff | Foreground color for off switch                                 |
| **int**     | colorForeOn  | Foreground color for on switch                                  |
| **String**  | textBackOff  | Background text for off switch                                  |
| **String**  | textBackOn   | Background text for on switch                                   |
| **String**  | textForeOff  | Foreground text for off switch                                  |
| **String**  | textForeOn   | Foreground text for on switch                                   |

### Methods

| Type            | Name                       | Description                       |
| --------------- | -------------------------- | --------------------------------- |
| **Constructor** | Switch( )                  | Creates a simple switch           |
| **int**         | getPreferredHeight( )      | Returns the switch minimum height |
| **int**         | getPreferredWidth( )       | Returns the switch minimum width  |
| **boolean**     | isOn( )                    | Return true if the switch is on   |
| **void**        | moveSwitch(boolean toLeft) | If true, move the switch to left  |
| **void**        | setOn(boolean b)           | If true, turn on the switch       |
| **void**        | setPos(int x, int y)       | Set the Switch´s x and y position |

### **References**

* See the [JavaDocs](https://rs.totalcross.com/doc/totalcross/ui/Switch.html) for more information.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.totalcross.com/master/documentation/components/switch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
