public class

Switch

extends View
java.lang.Object
   ↳ com.neomades.ui.View
     ↳ com.neomades.ui.Switch

Class Overview

Switch component shows an On/Off button.

The user manipulates this component by dragging horizontally the slider part ("flips" it).

You can customize the appearance of the switch by changing the on/off texts, and colors.

Handling switch changes

Default properties

By default, the following properties are applied to a new TimePicker:

  • focusable (limited to some devices)
  • clickable
  • enabled
  • not checked
  • platform default checked color
  • width content stretch : MATCH_CONTENT.
  • height content stretch : MATCH_CONTENT.
  • content alignment : HCENTER|VCENTER.

Cross Platform Considerations

  • In Android, the switch shows a toggle button in older versions (before API Level 14), otherwise it shows the Holo switch.

Summary

XML Attributes
Attribute Name Related Method Description
checked setChecked(boolean) To check the checkbox  
checkedColor setCheckedColor(Color) #AARRGGBB or a color  
contentAlignment setContentAlignment(int) A combination of alignment values (eg: TOP|LEFT)  
defaultText setDefaultText(String) Res.string.XXXX or a text  
font setFont(Font) Font resource id: Res.font.FONT_XXXX  
textColor setTextColor(Color) #AARRGGBB or a color  
textOff setTextOff(String) Res.string.XXXX or a text  
textOn setTextOn(String) Res.string.XXXX or a text  
textSize setTextSize(int) a size in dp  
[Expand]
Inherited XML Attributes
From class com.neomades.ui.View
[Expand]
Inherited Constants
From interface com.neomades.ui.Alignment
From interface com.neomades.ui.StretchMode
Public Constructors
Switch()
Construct a new Switch or Toggle (old version of android)
Public Methods
boolean isChecked()
void setCheckChangedListener(CheckChangedListener checkChangedListener)
Registers a callback to be invoked when the checked state of this switch changes.
void setChecked(boolean checked)
Change the checked state of the view to on (true) or off ( false).
void setCheckedColor(Color color)
Sets the color of the Switch when it is checked.
void setContentAlignment(int alignment)
Sets the alignment of the text in the Switch.
void setDefaultText(String text)
Sets the default text of the Switch.
void setDefaultText(int resId)
Sets the default text of the Switch.
void setFont(Font font)
Sets the Switch text font.
void setTextColor(Color color)
Sets the Switch's text color.
void setTextOff(String off)
Sets the text displayed when the button is not in the checked state.
void setTextOff(int resId)
Sets the text displayed when the button is in the unchecked state.
void setTextOn(int resId)
Sets the text displayed when the button is in the checked state.
void setTextOn(String on)
Sets the text displayed when the button is in the checked state.
void setTextSize(int sizeInDp)
Sets the text size to a given value (in dp).
void toggle()
Change the checked state of the view to the inverse of its current state.
[Expand]
Inherited Methods
From class com.neomades.ui.View
From class java.lang.Object

XML Attributes

checked

To check the checkbox

Related Methods

checkedColor

#AARRGGBB or a color

Related Methods

contentAlignment

A combination of alignment values (eg: TOP|LEFT)

Related Methods

defaultText

Res.string.XXXX or a text

Related Methods

font

Font resource id: Res.font.FONT_XXXX

Related Methods

textColor

#AARRGGBB or a color

Related Methods

textOff

Res.string.XXXX or a text

Related Methods

textOn

Res.string.XXXX or a text

Related Methods

textSize

a size in dp

Related Methods

Public Constructors

public Switch ()

Construct a new Switch or Toggle (old version of android)

Public Methods

public boolean isChecked ()

Returns
  • if the switch state is on

public void setCheckChangedListener (CheckChangedListener checkChangedListener)

Registers a callback to be invoked when the checked state of this switch changes.

Parameters
checkChangedListener the callback to be called or null to unregister

public void setChecked (boolean checked)

Change the checked state of the view to on (true) or off ( false).

Related XML Attributes
Parameters
checked true to change the state to on
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setCheckedColor (Color color)

Sets the color of the Switch when it is checked.

Related XML Attributes
Parameters
color the color of the Switch in the checked state
Throws
CrossThreadException if the method is called from outside the UI-Thread
NullPointerException if the color parameter is null

public void setContentAlignment (int alignment)

Sets the alignment of the text in the Switch. The parameter is a combination of values from Alignment

Related XML Attributes
Parameters
alignment The new alignment of the button's text
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setDefaultText (String text)

Sets the default text of the Switch.

Limitations

This method has an effect only in Android.

Related XML Attributes
Parameters
text the label string
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setDefaultText (int resId)

Sets the default text of the Switch.

Limitations

This method has an effect only in Android.

Parameters
resId the string resource id
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setFont (Font font)

Sets the Switch text font.

Related XML Attributes
Parameters
font new font for the text
Throws
CrossThreadException if the method is called from outside the UI-Thread
NullPointerException if the font parameter is null

public void setTextColor (Color color)

Sets the Switch's text color.

Related XML Attributes
Parameters
color new color for the text
Throws
CrossThreadException if the method is called from outside the UI-Thread
NullPointerException if the parameter is null

public void setTextOff (String off)

Sets the text displayed when the button is not in the checked state.

Related XML Attributes
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setTextOff (int resId)

Sets the text displayed when the button is in the unchecked state.

Parameters
resId the string resource id
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setTextOn (int resId)

Sets the text displayed when the button is in the checked state.

Parameters
resId the string resource id
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setTextOn (String on)

Sets the text displayed when the button is in the checked state.

Related XML Attributes
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setTextSize (int sizeInDp)

Sets the text size to a given value (in dp).

Related XML Attributes
Parameters
sizeInDp size to set to the text

public void toggle ()

Change the checked state of the view to the inverse of its current state.

Throws
CrossThreadException if the method is called from outside the UI-Thread