Easy Tutorial
❮ Verilog2 Codeguide Cpp Inline Usage ❯

2.3.6 ToggleButton and Switch

Category Android Basic Tutorial

Introduction:

>

This section introduces the basic Android UI components: ToggleButton and Switch. These components might be unfamiliar to some, but I remember my first outsourcing company used a TextView for a wifi connection toggle, with two images designed by the artist for the on and off states, and set programmatically. When the TextView was clicked, it checked the state and set the corresponding background...

Well, that was quite something. Anyway, both of these are switch components, but the latter requires Android 4.0 or later, so the minsdk in AndroidManifest.xml needs to be >= 14, otherwise it will throw an error. Let's first see what these controls look like. After Android 5.0, these controls have become much better looking compared to previous versions. Here's how they looked before 5.0:

ToggleButton and Switch before 5.0: 5.0 version:

A stark contrast... Next, let's learn how to use these two controls, as their usage is almost identical.

Before we start, here are the official API references: Switch ; ToggleButton


1. Core Attributes:

1) ToggleButton

Attributes available for setting:

>

-android:disabledAlpha: Sets the transparency of the button when disabled

-android:textOff: The text displayed when the button is not selected

-android:textOn: The text displayed when the button is selected Additionally, you can create a selector and set the Background attribute.

2) Switch

Attributes available for setting:

>

-android:showText: Sets whether text is displayed on/off, boolean

-android:splitTrack: Whether to set a gap to separate the thumb from the bottom image, boolean

-android:switchMinWidth: Sets the minimum width of the switch

-android:switchPadding: Sets the spacing of text inside the thumb

-android:switchTextAppearance: Sets the text appearance of the switch, not sure what it does...

-android:textOff: The text displayed when the button is not selected

-android:textOn: The text displayed when the button is selected

-android:textStyle: The text style, bold, italic, underline, etc.

-android:track: The bottom image

-android:thumb: The thumb image

-android:typeface: Sets the font, default supports these three: sans, serif, monospace; You can also use other font files (*.ttf), first save the font file in assets/fonts/ directory, but need to set in Java code: Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"); textView.setTypeface(typeFace);


2. Usage Example:

Since it's quite simple, we'll put them together, and also set the thumb and bottom images for the Switch to achieve a look similar to IOS 7's slider, but there's a drawback that you can't set the size of the thumb and bottom in XML, the Switch will be as big as the material, we can get the Drawable object in Java and modify the size, a simple example:

Running Effect:

WeChat Subscription

❮ Verilog2 Codeguide Cpp Inline Usage ❯