Toggle Button Tutorial in Android using Basic4Android

Good day! This is my other tutorial on views/controls in Android using Basic4Android, the Toggle Button. A toggle button allows the user to change a setting between two states. You can add a basic toggle button to your layout with the ToggleButton object. On this, you need to create one button and named it as "tb" . Next, named your abstract design as "main". Your abstract designer will be like this one below: abstractdesign Declare your variable in the Sub_Global like this:
  1. Sub Globals
  2.         'These global variables will be redeclared each time the activity is created.
  3.         'These variables can only be accessed from this module.
  4.         Dim tb As ToggleButton
  5. End Sub
We declare tb as our variable for ToggleButton to be called by the other methods. Next, download the image or save it as image in your computer, this image below: For Off Button btnOff For On Button btnOn Now create a new activity. Type the following code below:
  1. Sub Activity_Create(FirstTime As Boolean)
  2.  
  3.         Activity.LoadLayout ("main")
  4.     'tb.Initialize("") 'no events will be caught
  5.         'determine color or bitmap drawable
  6.         Dim checked, unchecked As BitmapDrawable
  7. '    Dim checked, unchecked As ColorDrawable
  8.        
  9.         'bitmap state
  10.         checked.Initialize (LoadBitmap(File.DirAssets, "button_on.png"))
  11.         unchecked.Initialize (LoadBitmap(File.DirAssets, "button_off.png"))
  12.    
  13.         'colors state
  14. '       checked.Initialize(Colors.Green, 10dip)
  15. '    unchecked.Initialize(Colors.Red, 10dip)
  16.  
  17.     Dim sld As StateListDrawable
  18.         'initialize statelistdrawable
  19.         sld.Initialize
  20.         'add states
  21.     sld.AddState(sld.State_Checked, checked)
  22.     sld.AddState(sld.State_Unchecked, unchecked)
  23.         'set background as state
  24.     tb.Background = sld
  25.  
  26. End Sub
Dim checked, unchecked As BitmapDrawable - this syntax holds the value of our variables checked and unchecked to have an image. Dim checked, unchecked As ColorDrawable - this syntax will also trigger to changed its color as it can hold color values. You can use this as your toggle if you don't want to have images in switching on and off. Dim sld As StateListDrawable - this syntax initializes sld as our StateListDrawable. StateListDrawable is a drawable objects that holds other drawables. Based on the view's current state a child drawable is selected. StateListDrawable holds a list of states. States can be "disabled", "enabled", "checked" and so on. A state can also be a combination of other states (like enabled and pressed).The StateListDrawable holds a list of states and drawables pairs. Whenever the view's state changes the list is scanned and the first item that matches the current state is chosen. Matching means that all elements in the state item are included in the current state.This means that an empty state item will match all current states.The order of states added is very important as the first item matches will be chosen. Thus, we coded this sld.AddState(sld.State_Checked, checked) and sld.AddState(sld.State_Unchecked, unchecked) tb.Background = sld - this syntax holds the background of our Toggle button will be equal to the value of our StateListDrawable. Now your activity when run will look like this: firstRun Note that when you run the application, the Toggle button will be in its Off value. Next, when you click the Toggle Button to get it on, it will look like this: Final Image Download the source code below and try it! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer STI College - Surigao City Mobile: 09488225971 E-mail:[email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky

Add new comment