Switch Design For Android App
Switch
public class Switch
extends CompoundButton
java.lang.Object | |||||
↳ | android.view.View | ||||
↳ | android.widget.TextView | ||||
↳ | android.widget.Button | ||||
↳ | android.widget.CompoundButton | ||||
↳ | android.widget.Switch |
A Switch is a two-state toggle switch widget that can select between two options. The user may drag the "thumb" back and forth to choose the selected option, or simply tap to toggle as if it were a checkbox. The text
property controls the text displayed in the label for the switch, whereas the off
and on
text controls the text on the thumb. Similarly, the textAppearance
and the related setTypeface() methods control the typeface and style of label text, whereas the switchTextAppearance
and the related setSwitchTypeface() methods control that of the thumb.
SwitchCompat
is a version of the Switch widget which runs on devices back to API 7.
See the Toggle Buttons guide.
Summary
XML attributes | |
---|---|
android:showText | Whether to draw on/off text. |
android:splitTrack | Whether to split the track and leave a gap for the thumb drawable. |
android:switchMinWidth | Minimum width for the switch component. |
android:switchPadding | Minimum space between the switch and caption text. |
android:switchTextAppearance | TextAppearance style for text displayed on the switch thumb. |
android:textOff | Text to use when the switch is in the unchecked/"off" state. |
android:textOn | Text to use when the switch is in the checked/"on" state. |
android:textStyle | Style (normal, bold, italic, bold|italic) for the text. |
android:thumb | Drawable to use as the "thumb" that switches back and forth. |
android:thumbTextPadding | Amount of padding on either side of text within the switch thumb. |
android:thumbTint | Tint to apply to the thumb. |
android:thumbTintMode | Blending mode used to apply the thumb tint. |
android:track | Drawable to use as the "track" that the switch thumb slides within. |
android:trackTint | Tint to apply to the track. |
android:trackTintMode | Blending mode used to apply the track tint. |
android:typeface | Typeface (normal, sans, serif, monospace) for the text. |
Inherited XML attributes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
|
Inherited constants | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
|
Inherited fields | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class
|
Public constructors | |
---|---|
Switch(Context context) Construct a new Switch with default styling. | |
Switch(Context context, AttributeSet attrs) Construct a new Switch with default styling, overriding specific style attributes as requested. | |
Switch(Context context, AttributeSet attrs, int defStyleAttr) Construct a new Switch with a default style determined by the given theme attribute, overriding specific style attributes as requested. | |
Switch(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) Construct a new Switch with a default style determined by the given theme attribute or style resource, overriding specific style attributes as requested. |
Public methods | |
---|---|
void | draw(Canvas c) Manually render this view (and all of its children) to the given Canvas. |
void | drawableHotspotChanged(float x, float y) This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view. |
CharSequence | getAccessibilityClassName() Return the class name of this object to be used for accessibility purposes. |
int | getCompoundPaddingLeft() Returns the left padding of the view, plus space for the left Drawable if any. |
int | getCompoundPaddingRight() Returns the right padding of the view, plus space for the right Drawable if any. |
boolean | getShowText() |
boolean | getSplitTrack() Returns whether the track should be split by the thumb. |
int | getSwitchMinWidth() Get the minimum width of the switch in pixels. |
int | getSwitchPadding() Get the amount of horizontal padding between the switch and the associated text. |
CharSequence | getTextOff() Returns the text displayed when the button is not in the checked state. |
CharSequence | getTextOn() Returns the text displayed when the button is in the checked state. |
Drawable | getThumbDrawable() Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track. |
int | getThumbTextPadding() Get the horizontal padding around the text drawn on the switch itself. |
BlendMode | getThumbTintBlendMode() |
ColorStateList | getThumbTintList() |
PorterDuff.Mode | getThumbTintMode() |
Drawable | getTrackDrawable() Get the drawable used for the track that the switch slides within. |
BlendMode | getTrackTintBlendMode() |
ColorStateList | getTrackTintList() |
PorterDuff.Mode | getTrackTintMode() |
void | jumpDrawablesToCurrentState() Call |
void | onMeasure(int widthMeasureSpec, int heightMeasureSpec) Measure the view and its content to determine the measured width and the measured height. |
boolean | onTouchEvent(MotionEvent ev) Implement this method to handle touch screen motion events. |
void | setChecked(boolean checked) Changes the checked state of this button. |
void | setShowText(boolean showText) Sets whether the on/off text should be displayed. |
void | setSplitTrack(boolean splitTrack) Specifies whether the track should be split by the thumb. |
void | setSwitchMinWidth(int pixels) Set the minimum width of the switch in pixels. |
void | setSwitchPadding(int pixels) Set the amount of horizontal padding between the switch and the associated text. |
void | setSwitchTextAppearance(Context context, int resid) Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource. |
void | setSwitchTypeface(Typeface tf) Sets the typeface in which the text should be displayed on the switch. |
void | setSwitchTypeface(Typeface tf, int style) Sets the typeface and style in which the text should be displayed on the switch, and turns on the fake bold and italic bits in the Paint if the Typeface that you provided does not have all the bits in the style that you specified. |
void | setTextOff(CharSequence textOff) Sets the text displayed when the button is not in the checked state. |
void | setTextOn(CharSequence textOn) Sets the text displayed when the button is in the checked state. |
void | setThumbDrawable(Drawable thumb) Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track. |
void | setThumbIcon(Icon icon) Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track - to the specified Icon. |
void | setThumbResource(int resId) Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track. |
void | setThumbTextPadding(int pixels) Set the horizontal padding around the text drawn on the switch itself. |
void | setThumbTintBlendMode(BlendMode blendMode) Specifies the blending mode used to apply the tint specified by |
void | setThumbTintList(ColorStateList tint) Applies a tint to the thumb drawable. |
void | setThumbTintMode(PorterDuff.Mode tintMode) Specifies the blending mode used to apply the tint specified by |
void | setTrackDrawable(Drawable track) Set the drawable used for the track that the switch slides within. |
void | setTrackIcon(Icon icon) Set the drawable used for the track that the switch slides within to the specified Icon. |
void | setTrackResource(int resId) Set the drawable used for the track that the switch slides within. |
void | setTrackTintBlendMode(BlendMode blendMode) Specifies the blending mode used to apply the tint specified by |
void | setTrackTintList(ColorStateList tint) Applies a tint to the track drawable. |
void | setTrackTintMode(PorterDuff.Mode tintMode) Specifies the blending mode used to apply the tint specified by |
void | toggle() Change the checked state of the view to the inverse of its current state |
Protected methods | |
---|---|
void | drawableStateChanged() This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown. |
int[] | onCreateDrawableState(int extraSpace) Generate the new |
void | onDraw(Canvas canvas) Implement this to do your drawing. |
void | onLayout(boolean changed, int left, int top, int right, int bottom) Called from layout when this view should assign a size and position to each of its children. |
boolean | verifyDrawable(Drawable who) If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. |
Inherited methods | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From interface
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From interface
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From interface
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From interface
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From interface
|
XML attributes
android:showText
Whether to draw on/off text.
May be a boolean value, such as "true
" or "false
".
Related methods:
-
setShowText(boolean)
android:splitTrack
Whether to split the track and leave a gap for the thumb drawable.
May be a boolean value, such as "true
" or "false
".
Related methods:
-
setSplitTrack(boolean)
android:switchMinWidth
Minimum width for the switch component.
May be a dimension value, which is a floating point number appended with a unit such as "14.5sp
". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).
Related methods:
-
setSwitchMinWidth(int)
android:switchPadding
Minimum space between the switch and caption text.
May be a dimension value, which is a floating point number appended with a unit such as "14.5sp
". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).
Related methods:
-
setSwitchPadding(int)
android:switchTextAppearance
TextAppearance style for text displayed on the switch thumb.
May be a reference to another resource, in the form "@[+][package:]type/name
" or a theme attribute in the form "?[package:]type/name
".
Related methods:
-
setSwitchTextAppearance(Context,int)
android:textOff
Text to use when the switch is in the unchecked/"off" state.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
Related methods:
-
setTextOff(CharSequence)
android:textOn
Text to use when the switch is in the checked/"on" state.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
Related methods:
-
setTextOn(CharSequence)
android:textStyle
Style (normal, bold, italic, bold|italic) for the text.
Must be one or more (separated by '|') of the following constant values.
Constant | Value | Description |
---|---|---|
bold | 1 | |
italic | 2 | |
normal | 0 |
Related methods:
-
setSwitchTypeface(Typeface)
android:thumb
Drawable to use as the "thumb" that switches back and forth.
May be a reference to another resource, in the form "@[+][package:]type/name
" or a theme attribute in the form "?[package:]type/name
".
Related methods:
-
setThumbResource(int)
android:thumbTextPadding
Amount of padding on either side of text within the switch thumb.
May be a dimension value, which is a floating point number appended with a unit such as "14.5sp
". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).
Related methods:
-
setThumbTextPadding(int)
android:thumbTint
Tint to apply to the thumb.
May be a color value, in the form of "#rgb
", "#argb
", "#rrggbb
", or "#aarrggbb
".
Related methods:
-
setThumbTintList(ColorStateList)
android:thumbTintMode
Blending mode used to apply the thumb tint.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
add | 10 | Combines the tint and drawable color and alpha channels, clamping the result to valid color values. Saturate(S + D) |
multiply | e | Multiplies the color and alpha channels of the drawable with those of the tint. [Sa * Da, Sc * Dc] |
screen | f | [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] |
src_atop | 9 | The tint is drawn above the drawable, but with the drawable's alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc] |
src_in | 5 | The tint is masked by the alpha channel of the drawable. The drawable's color channels are thrown out. [Sa * Da, Sc * Da] |
src_over | 3 | The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc] |
Related methods:
-
setThumbTintMode(PorterDuff.Mode)
android:track
Drawable to use as the "track" that the switch thumb slides within.
May be a reference to another resource, in the form "@[+][package:]type/name
" or a theme attribute in the form "?[package:]type/name
".
Related methods:
-
setTrackResource(int)
android:trackTint
Tint to apply to the track.
May be a color value, in the form of "#rgb
", "#argb
", "#rrggbb
", or "#aarrggbb
".
Related methods:
-
setTrackTintList(ColorStateList)
android:trackTintMode
Blending mode used to apply the track tint.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
add | 10 | Combines the tint and drawable color and alpha channels, clamping the result to valid color values. Saturate(S + D) |
multiply | e | Multiplies the color and alpha channels of the drawable with those of the tint. [Sa * Da, Sc * Dc] |
screen | f | [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] |
src_atop | 9 | The tint is drawn above the drawable, but with the drawable's alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc] |
src_in | 5 | The tint is masked by the alpha channel of the drawable. The drawable's color channels are thrown out. [Sa * Da, Sc * Da] |
src_over | 3 | The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc] |
Related methods:
-
setTrackTintMode(PorterDuff.Mode)
android:typeface
Typeface (normal, sans, serif, monospace) for the text.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
monospace | 3 | |
normal | 0 | |
sans | 1 | |
serif | 2 |
Related methods:
-
setSwitchTypeface(Typeface)
Public constructors
Switch
public Switch (Context context)
Construct a new Switch with default styling.
Parameters | |
---|---|
context | Context : The Context that will determine this widget's theming. |
Switch
public Switch (Context context, AttributeSet attrs)
Construct a new Switch with default styling, overriding specific style attributes as requested.
Parameters | |
---|---|
context | Context : The Context that will determine this widget's theming. |
attrs | AttributeSet : Specification of attributes that should deviate from default styling. |
Switch
public Switch (Context context, AttributeSet attrs, int defStyleAttr)
Construct a new Switch with a default style determined by the given theme attribute, overriding specific style attributes as requested.
Parameters | |
---|---|
context | Context : The Context that will determine this widget's theming. |
attrs | AttributeSet : Specification of attributes that should deviate from the default styling. |
defStyleAttr | int : An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults. |
Switch
public Switch (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Construct a new Switch with a default style determined by the given theme attribute or style resource, overriding specific style attributes as requested.
Parameters | |
---|---|
context | Context : The Context that will determine this widget's theming. |
attrs | AttributeSet : Specification of attributes that should deviate from the default styling. |
defStyleAttr | int : An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults. |
defStyleRes | int : A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults. |
Public methods
draw
public void draw (Canvas c)
Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas)
instead of overriding this method. If you do need to override this method, call the superclass version.
If you override this method you must call through to the superclass implementation.
Parameters | |
---|---|
c | Canvas : The Canvas to which the View is rendered. |
drawableHotspotChanged
public void drawableHotspotChanged (float x, float y)
This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.
Dispatching to child views is handled by dispatchDrawableHotspotChanged(float, float)
.
Be sure to call through to the superclass when overriding this function.
If you override this method you must call through to the superclass implementation.
Parameters | |
---|---|
x | float : hotspot x coordinate |
y | float : hotspot y coordinate |
getAccessibilityClassName
public CharSequence getAccessibilityClassName ()
Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName
.
Returns | |
---|---|
CharSequence |
getCompoundPaddingLeft
public int getCompoundPaddingLeft ()
Returns the left padding of the view, plus space for the left Drawable if any.
Returns | |
---|---|
int |
getCompoundPaddingRight
public int getCompoundPaddingRight ()
Returns the right padding of the view, plus space for the right Drawable if any.
Returns | |
---|---|
int |
getShowText
public boolean getShowText ()
Related XML Attributes:
- android:showText
Returns | |
---|---|
boolean | whether the on/off text should be displayed |
getSplitTrack
public boolean getSplitTrack ()
Returns whether the track should be split by the thumb.
Related XML Attributes:
- android:splitTrack
Returns | |
---|---|
boolean |
getSwitchMinWidth
public int getSwitchMinWidth ()
Get the minimum width of the switch in pixels. The switch's width will be the maximum of this value and its measured width as determined by the switch drawables and text used.
Related XML Attributes:
- android:switchMinWidth
Returns | |
---|---|
int | Minimum width of the switch in pixels |
getSwitchPadding
public int getSwitchPadding ()
Get the amount of horizontal padding between the switch and the associated text.
Related XML Attributes:
- android:switchPadding
Returns | |
---|---|
int | Amount of padding in pixels |
getThumbDrawable
public Drawable getThumbDrawable ()
Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
Related XML Attributes:
- android:thumb
Returns | |
---|---|
Drawable | Thumb drawable |
getThumbTextPadding
public int getThumbTextPadding ()
Get the horizontal padding around the text drawn on the switch itself.
Related XML Attributes:
- android:thumbTextPadding
Returns | |
---|---|
int | Horizontal padding for switch thumb text in pixels |
getThumbTintBlendMode
public BlendMode getThumbTintBlendMode ()
Related XML Attributes:
- android:thumbTintMode
Returns | |
---|---|
BlendMode | the blending mode used to apply the tint to the thumb drawable This value may be null . |
getTrackDrawable
public Drawable getTrackDrawable ()
Get the drawable used for the track that the switch slides within.
Related XML Attributes:
- android:track
Returns | |
---|---|
Drawable | Track drawable |
getTrackTintBlendMode
public BlendMode getTrackTintBlendMode ()
Related XML Attributes:
- android:trackTintMode
Returns | |
---|---|
BlendMode | the blending mode used to apply the tint to the track drawable This value may be null . |
jumpDrawablesToCurrentState
public void jumpDrawablesToCurrentState ()
Call Drawable.jumpToCurrentState()
on all Drawable objects associated with this view.
Also calls StateListAnimator#jumpToCurrentState()
if there is a StateListAnimator attached to this view.
If you override this method you must call through to the superclass implementation.
onMeasure
public void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int)
and should be overridden by subclasses to provide accurate and efficient measurement of their contents.
CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int)
to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException
, thrown by measure(int, int)
. Calling the superclass' onMeasure(int, int)
is a valid use.
The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int)
to provide better measurements of their content.
If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight()
and getSuggestedMinimumWidth()
).
Parameters | |
---|---|
widthMeasureSpec | int : horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec . |
heightMeasureSpec | int : vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec . |
onTouchEvent
public boolean onTouchEvent (MotionEvent ev)
Implement this method to handle touch screen motion events.
If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick()
. This will ensure consistent system behavior, including:
- obeying click sound preferences
- dispatching OnClickListener calls
- handling
ACTION_CLICK
when accessibility features are enabled
Parameters | |
---|---|
ev | MotionEvent : The motion event. |
Returns | |
---|---|
boolean | True if the event was handled, false otherwise. |
setChecked
public void setChecked (boolean checked)
Changes the checked state of this button.
Parameters | |
---|---|
checked | boolean : true to check the button, false to uncheck it |
setShowText
public void setShowText (boolean showText)
Sets whether the on/off text should be displayed.
Related XML Attributes:
- android:showText
Parameters | |
---|---|
showText | boolean : true to display on/off text |
setSplitTrack
public void setSplitTrack (boolean splitTrack)
Specifies whether the track should be split by the thumb. When true, the thumb's optical bounds will be clipped out of the track drawable, then the thumb will be drawn into the resulting gap.
Related XML Attributes:
- android:splitTrack
Parameters | |
---|---|
splitTrack | boolean : Whether the track should be split by the thumb |
setSwitchMinWidth
public void setSwitchMinWidth (int pixels)
Set the minimum width of the switch in pixels. The switch's width will be the maximum of this value and its measured width as determined by the switch drawables and text used.
Related XML Attributes:
- android:switchMinWidth
Parameters | |
---|---|
pixels | int : Minimum width of the switch in pixels |
setSwitchPadding
public void setSwitchPadding (int pixels)
Set the amount of horizontal padding between the switch and the associated text.
Related XML Attributes:
- android:switchPadding
Parameters | |
---|---|
pixels | int : Amount of padding in pixels |
setSwitchTextAppearance
public void setSwitchTextAppearance (Context context, int resid)
Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
Related XML Attributes:
- android:switchTextAppearance
Parameters | |
---|---|
context | Context |
resid | int |
setSwitchTypeface
public void setSwitchTypeface (Typeface tf, int style)
Sets the typeface and style in which the text should be displayed on the switch, and turns on the fake bold and italic bits in the Paint if the Typeface that you provided does not have all the bits in the style that you specified.
Parameters | |
---|---|
tf | Typeface |
style | int |
setTextOff
public void setTextOff (CharSequence textOff)
Sets the text displayed when the button is not in the checked state.
Related XML Attributes:
- android:textOff
Parameters | |
---|---|
textOff | CharSequence |
setTextOn
public void setTextOn (CharSequence textOn)
Sets the text displayed when the button is in the checked state.
Related XML Attributes:
- android:textOn
Parameters | |
---|---|
textOn | CharSequence |
setThumbDrawable
public void setThumbDrawable (Drawable thumb)
Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
Related XML Attributes:
- android:thumb
Parameters | |
---|---|
thumb | Drawable : Thumb drawable |
setThumbIcon
public void setThumbIcon (Icon icon)
Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track - to the specified Icon.
Parameters | |
---|---|
icon | Icon : an Icon holding the desired thumb, or null to clear the thumb This value may be null . |
setThumbResource
public void setThumbResource (int resId)
Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
Related XML Attributes:
- android:thumb
Parameters | |
---|---|
resId | int : Resource ID of a thumb drawable |
setThumbTextPadding
public void setThumbTextPadding (int pixels)
Set the horizontal padding around the text drawn on the switch itself.
Related XML Attributes:
- android:thumbTextPadding
Parameters | |
---|---|
pixels | int : Horizontal padding for switch thumb text in pixels |
setTrackDrawable
public void setTrackDrawable (Drawable track)
Set the drawable used for the track that the switch slides within.
Related XML Attributes:
- android:track
Parameters | |
---|---|
track | Drawable : Track drawable |
setTrackIcon
public void setTrackIcon (Icon icon)
Set the drawable used for the track that the switch slides within to the specified Icon.
Parameters | |
---|---|
icon | Icon : an Icon holding the desired track, or null to clear the track This value may be null . |
setTrackResource
public void setTrackResource (int resId)
Set the drawable used for the track that the switch slides within.
Related XML Attributes:
- android:track
Parameters | |
---|---|
resId | int : Resource ID of a track drawable |
toggle
public void toggle ()
Change the checked state of the view to the inverse of its current state
Protected methods
drawableStateChanged
protected void drawableStateChanged ()
This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.
If the View has a StateListAnimator, it will also be called to run necessary state change animations.
Be sure to call through to the superclass when overriding this function.
If you override this method you must call through to the superclass implementation.
onCreateDrawableState
protected int[] onCreateDrawableState (int extraSpace)
Generate the new Drawable
state for this view. This is called by the view system when the cached Drawable state is determined to be invalid. To retrieve the current state, you should use getDrawableState()
.
Parameters | |
---|---|
extraSpace | int : if non-zero, this is the number of extra entries you would like in the returned array in which you can place your own states. |
Returns | |
---|---|
int[] | Returns an array holding the current Drawable state of the view. |
onDraw
protected void onDraw (Canvas canvas)
Implement this to do your drawing.
Parameters | |
---|---|
canvas | Canvas : the canvas on which the background will be drawn |
onLayout
protected void onLayout (boolean changed, int left, int top, int right, int bottom)
Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.
Parameters | |
---|---|
changed | boolean : This is a new size or position for this view |
left | int : Left position, relative to parent |
top | int : Top position, relative to parent |
right | int : Right position, relative to parent |
bottom | int : Bottom position, relative to parent |
verifyDrawable
protected boolean verifyDrawable (Drawable who)
If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.
Be sure to call through to the super class when overriding this function.
If you override this method you must call through to the superclass implementation.
Parameters | |
---|---|
who | Drawable : This value cannot be null . |
Returns | |
---|---|
boolean | boolean If true then the Drawable is being displayed in the view; else false and it is not allowed to animate. |
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2021-10-27 UTC.
Switch Design For Android App
Source: https://developer.android.com/reference/android/widget/Switch
Posted by: blacksaight.blogspot.com
0 Response to "Switch Design For Android App"
Post a Comment