• We just launched and are currently in beta. Join us as we build and grow the community.

Toggle Button Tutorial in Android using Basic4Android

thebouncedrops

Comic Relief Officer
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
47
Likes
59
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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_2.png


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
button_off.png


For On Button
button_on.png


Now create a new activity. Type the following code below:
  1. Sub

    Activity_Create(FirstTime As

    Boolean

    )

  2. Activity.LoadLayout ("main"

    )
  3. 'tb.Initialize("") 'no events will be caught
  4. 'determine color or bitmap drawable
  5. Dim

    checked, unchecked As

    BitmapDrawable
  6. ' Dim checked, unchecked As ColorDrawable

  7. 'bitmap state
  8. checked.Initialize (LoadBitmap(File.DirAssets, "button_on.png"

    ))
  9. unchecked.Initialize (LoadBitmap(File.DirAssets, "button_off.png"

    ))

  10. 'colors state
  11. ' checked.Initialize(Colors.Green, 10dip)
  12. ' unchecked.Initialize(Colors.Red, 10dip)

  13. Dim

    sld As

    StateListDrawable
  14. 'initialize statelistdrawable
  15. sld.Initialize
  16. 'add states
  17. sld.AddState(sld.State_Checked, checked)
  18. sld.AddState(sld.State_Unchecked, unchecked)
  19. 'set background as state
  20. tb.Background = sld

  21. 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_2.png


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.png


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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,270

322,965

322,974

Top