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:
- 1.5.1 Git Tutorial: Basic Operations with Local Repositories
- 1.5.2 Using GitHub to Set Up a Remote Repository with Git
- 1.6 How to Use the 9(Nine Sisters) Image
- 1.7 Interface Prototype Design
- 1.8 Project Analysis (Various Files, Resource Access)
- 1.9 Android App Signing and Packaging
- 1.11 Decompiling APK to Obtain Code & Resources
- 2.1 Concepts of View and ViewGroup
- 2.2.1 LinearLayout (Linear Layout)
- 2.2.2 RelativeLayout (Relative Layout)
- 2.2.3 TableLayout (Table Layout)
- 2.2.4 FrameLayout (Frame Layout)
- 2.2.5 GridLayout (Grid Layout)
- 2.2.6 AbsoluteLayout (Absolute Layout)
- 2.3.1 Detailed Explanation of TextView (Text Field)
- 2.3.2 Detailed Explanation of EditText (Input Field)
- 2.3.3 Button and ImageButton
- 2.3.4 ImageView (Image View)
- 2.3.5 RadioButton (Radio Button) & Checkbox (Checkbox)
- 2.3.6 ToggleButton and Switch
- 2.3.7 ProgressBar (Progress Bar)
- 2.3.8 SeekBar (Drag Bar)
- 2.3.9 RatingBar (Star Rating Bar)
- 2.4.1 ScrollView (Scroll Bar)
- 2.4.2 Date & Time Components (Part 1)
- 2.4.3 Date & Time Components (Part 2)
- 2.4.4 Basic Adapter Explanation
- 2.4.5 Simple Usage of ListView
- 2.4.6 Optimization of BaseAdapter
- 2.4.7 Focus Issues with ListView
- 2.4.8 Solving Checkbox Misalignment in ListView
- 2.4.9 Data Update Issues in ListView
- 2.5.0 Building a Reusable Custom BaseAdapter
- 2.5.1 Implementing Multi-Layout ListView Items
- 2.5.2 Basic Usage of GridView (Grid View)
- 2.5.3 Basic Usage of Spinner (Dropdown List)
- 2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Field)
- 2.5.5 Basic Usage of ExpandableListView (Collapsible List)
- 2.5.6 Basic Usage of ViewFlipper (Flip View)
- 2.5.7 Basic Usage of Toast
- 2.5.8 Detailed Explanation of Notification (Status Bar Notification)
- 2.5.9 Detailed Explanation of AlertDialog (Dialog Box)
- 2.6.0 Basic Usage of Other Common Dialogs
- 2.6.1 Basic Usage of PopupWindow (Floating Box)
- 2.6.2 Menu (Menu)
- 2.6.3 Simple Usage of ViewPager
- 2.6.4 Simple Usage of DrawerLayout (Official Side Sliding Menu)
- 3.1.1 Event Handling Mechanism Based on Listeners
- 3.2 Event Handling Mechanism Based on Callbacks
- 3.3 Analysis of Handler Message Passing Mechanism
- 3.4 TouchListener vs OnTouchEvent + Multi-Touch
- 3.5 Listening for Content Changes in EditText
- 3.6 Responding to System Setting Events (Configuration Class)
- 3.7 AsyncTask Asynchronous Task
- 3.8 Gestures (Gestures)
- 4.1.1 Activity: Beginner's Guide
- 4.1.2 Activity: Intermediate Guide
- 4.1.3 Activity: Advanced Guide
- 4.2.1 Service: Introduction
- 4.2.2 Service: Intermediate
- 4.2.3 Service: Advanced
- 4.3.1 BroadcastReceiver: Basic
- 4.3.2 BroadcastReceiver: Advanced
- 4.4.1 ContentProvider: Introduction
- 4.4.2 ContentProvider: Advanced - Document Provider
- 4.5.1 Basic Usage of Intent
- 4.5.2 Passing Complex Data with Intent
- 5.1 Basic Overview of Fragment
- 5.2.1 Fragment Example: Bottom Navigation Bar Implementation (Method 1)
- 5.2.2 Fragment Example: Bottom Navigation Bar Implementation (Method 2)
- 5.2.3 Fragment Example: Bottom Navigation Bar Implementation (Method 3)
- 5.2.4 Fragment Example: Bottom Navigation Bar + ViewPager for Swiping Pages
- 5.2.5 Fragment Example: Simple Implementation of News/Shopping App List Fragment
- 6.1 Data Storage and Access: File Storage and Reading
- 6.2 Data Storage and Access: Saving User Preferences with SharedPreferences
- 6.3.1 Data Storage and Access: Introduction to SQLite Database
- 6.3.2 Data Storage and Access: Advanced SQLite Database
- 7.1.1 Android Network Programming: What to Learn and HTTP Protocol
- 7.1.2 Learning Android HTTP Request Headers and Response Headers
- 7.1.3 Android HTTP Request Method: HttpURLConnection
- 7.1.4 Android HTTP Request Method: HttpClient
- 7.2.1 Android XML Data Parsing
- 7.2.2 Android JSON Data Parsing
- 7.3.1 Android File Upload
- 7.3.2 Android File Download (1)
- 7.3.3 Android File Download (2)
- 7.4 Android Calling WebService
- 7.5.1 WebView (Web View) Basic Usage
- 7.5.2 WebView and JavaScript Interaction Basics
- 7.5.3 Important Considerations for WebView After Android 4.4
- 7.5.4 WebView File Download
- 7.5.5 WebView Cache Issues
- 7.5.6 WebView Handling Webpage Error Code Information
- 7.6.1 Socket Learning Network Basics Preparation
- 7.6.2 Socket Communication Based on TCP Protocol (1)
- 7.6.3 Socket Communication Based on TCP Protocol (2)
- 7.6.4 Socket Communication Based on UDP Protocol
- 8.1.1 Summary of 13 Drawable Types in Android Part 1
- 8.1.2 Summary of 13 Drawable Types in Android Part 2
- 8.1.3 Summary of 13 Drawable Types in Android Part 3
- 8.2.1 Bitmap (Bitmap) Comprehensive Analysis Part 1
- 8.2.2 OOM Issues Caused by Bitmap
- 8.3.1 Detailed Explanation of Three Drawing Tool Classes
- 8.3.2 Practical Examples of Drawing Classes
- 8.3.3 Paint API - MaskFilter (Mask)
- 8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 1)
- 8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 2)
- 8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 3)
- 8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 4)
- 8.3.8 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 5)
- 8.3.9 Paint API - ColorFilter (Color Filter) (1/3)
- 8.3.10 Paint API - ColorFilter (Color Filter) (2/3)
- 8.3.11 Paint API - ColorFilter (Color Filter) (3/3)
- 8.3.12 Paint API - PathEffect (Path Effect)
- 8.3.13 Paint API - Shader (Image Rendering)
- 8.3.14 Paint Enum/Constant Values and ShadowLayer Shadow Effects
- 8.3.15 Paint API - Typeface (Font Style)
- 8.3.16 Canvas API Detailed Explanation (Part 1)
- 8.3.17 Canvas API Detailed Explanation (Part 2) Clipping Methods Collection
- 8.3.18 Canvas API Detailed Explanation (Part 3) Matrix and drawBitmapMesh
- 8.4.1 Android Animation Collection - Frame Animation
- 8.4.2 Android Animation Collection - Tween Animation
- 8.4.3 Android Animation Collection - Property Animation - First Encounter
- 8.4.4 Android Animation Collection - Property Animation - Second Encounter
- 9.1 Using SoundPool to Play Sound Effects (Duang~)
- 9.2 MediaPlayer Playing Audio and Video
- 9.3 Using Camera to Take Photos
- 9.4 Using MediaRecord to Record Audio
- 10.1 TelephonyManager (Telephony Manager)
- 10.2 SmsManager (SMS Manager)
- 10.3 AudioManager (Audio Manager)
- 10.4 Vibrator (Vibrator)
- 10.5 AlarmManager (Alarm Service)
- 10.6 PowerManager (Power Service)
- 10.7 WindowManager (Window Management Service)
- 10.8 LayoutInflater (Layout Service)
- 10.9 WallpaperManager (Wallpaper Manager)
- 10.10 Sensor Topic (1) - Introduction
- 10.11 Sensor Topic (2) - Orientation Sensor
- 10.12 Sensor Topic (3) - Accelerometer/Gyroscope Sensor
- 10.12 Sensor Topic (4) - Understanding Other Sensors
- 10.14 Android GPS Introduction
- 11.0 "2015 Latest Android Basic Beginner Tutorial" Completion Celebration~
- 12.1 Android Practice: DrySister Viewing Girls Application (First Edition) - Project Setup and Simple Implementation
- 12.2 DrySister Viewing Girls Application (First Edition) - 2. Parsing Backend Data
- 12.3 DrySister Viewing Girls Application (First Edition) - 3. Image Loading Optimization (Writing an Image Cache Small Framework)
- 12.4 DrySister Viewing Girls Application (First Edition) - 4. Adding Data Cache (Introducing SQLite)
- 12.5 DrySister Viewing Girls Application (First Edition) - 5. Code Review, Adjustment, and Logging Class Writing
- 12.6 DrySister Viewing Girls Application (First Edition) - 6. Icon Creation, Obfuscation, Signing, APK Thinning, Application Release