Android Adventures - Basic Controls And Events
Written by Mike James   
Wednesday, 17 July 2013
Article Index
Android Adventures - Basic Controls And Events
General Event Handler
Checkboxes
Switches and Toggle Buttons

Switches and Toggle Buttons

Switches and Toggle buttons are just CheckBoxes in another format. The store one of two states and they change state when the user clicks on them - just like a CheckBox. 

toggle

You can check the state of a Switch/Toggle button using the isChecked method and you can use its onClick event to monitor when its state changes.  The only real difference is that you can use the textOn and textOff to set what is displayed when the switch/toggle is on or off. 

It is worth keeping in mind that the Switch is new in Android 4 and doesn't work in earlier versions. 

 

Radio Buttons 

The final member of the "simple" input controls is the RadioButton. This works exactly like a CheckBox in that it can be in one of two states but the big difference is that a set of RadioButtons works in a group and only one of them can be selected at a time. 

The reason for the term "radio button" is that in the early days of electronics car radios had mechanical tuning buttons arranged in a line which let the driver quickly select one station by pressing a button. When you pressed a new button the current button popped up so that only one button was pressed at any given moment - making sure that you only listened to one station at a time.

The only complication in using RadioButtons is making sure you group them together correctly. To do this we have to make use of a RadioGroup container which is used to hold all of the buttons that work together. 

 

Using Android Studio you can create a group of RadioButtons by first placing a RadioGroup container on the design surface and then placing as many RadioButtons inside the container as you require. You will see the RadioGroup outlined in orange when you hover the cursor over it ready to drop a RadioButton into it. If a group of RadioButtons don't work as you expect then the chances are you don't have all of the buttons within the RadioGroup. 

radio1

 

All of the RadioButtons within a RadioGroup automatically work so that only one button can be selected at a time - you don't have to do any extra work to implement this behaviour.

To find out which button is selected you can use the isChecked method as in the case of the CheckBox - in fact you can work with a set of RadioButtons in exactly the same way as a set of CheckBoxes with the only differences being the use of the RadioGroup and only one button can be selected at any one time. You can use the onClick event to detect when any button has been modified and the setChecked or the toggle methods to modify the state of a button. 

Androidgears

Android Adventures With Android Studio

Contents

  1. Getting Started With Android Studio
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Lifecycle and State
  5. Basic Controls And Events
  6. Spinners
  7. Pickers
  8. UI Graphics A Deep Dive
  9. ListView And Adapters
  10. Introducing Fragments
  11. Fragments and XML
  12. Fragment And Activity Working Together
  13. Managing Fragments
  14. Custom dialogs using DialogFragment
  15. Dialog Classes In DialogFragment
  16. A NumberPicker DialogFragment Project

 

Coming Next

In the next installment we'll discover how to use more advanced UI components - the spinners and pickers.

Meanwhile if you have any questions on what we've covered so far please let me know using the comments.

 

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus

 



Last Updated ( Friday, 17 October 2014 )
 
 

   
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.