View/Set ToggleButton State

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I am trying to set a toogle button state also view the current state of a toogle button and can't seem to work it out.

I have used the following code but it seem not to work.

B4X:
Dim ToggleButton1 As ToggleButton 
ToggleButton1.Checked = True

Any ideas?
 

derez

Expert
Licensed User
Longtime User
Your code is setting the togglebutton to true (meaning it is checked).
To see the status you can write:
B4X:
log(togglebutton1.checked)
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
It's not even changing the state.

I can't seem to turn it on or off unless I manually do it in the designer

EDIT: It seems to log the real state but doesn't update on the GUI
 
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
You need to Initialize your ToggleButton:

B4X:
Dim ToggleButton1 As ToggleButton 
ToggleButton1.Initialize("MyEventName")
ToggleButton1.Checked = True

Views created in the Designer are automatically Initialized - Views created in code must be manually Initialized.

Martin.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
You need to Initialize your ToggleButton:

B4X:
Dim ToggleButton1 As ToggleButton 
ToggleButton1.Initialize("MyEventName")
ToggleButton1.Checked = True

Views created in the Designer are automatically Initialized - Views created in code must be manually Initialized.

Martin.

I tried that still don't work.

My code now looks like this:

B4X:
Dim ToggleButton1 As ToggleButton 
ToggleButton1.Initialize("test1")
ToggleButton1.Checked = True
Log(ToggleButton1.checked)

The Log writes True but the ToogleButton still says Off and doesn't change.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
This works for me:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Dim Button1 As Button
   Dim ToggleButton1 As ToggleButton 
End Sub

Sub Activity_Create(FirstTime As Boolean)

   ToggleButton1.Initialize("test1")
   ToggleButton1.Checked = True
   Log(ToggleButton1.checked)
   Activity.AddView(ToggleButton1, 0, 0, 100dip, 100dip)

   Button1.Initialize("Button1")
   Button1.Text="Click"
   Activity.AddView(Button1, 0, 105dip, 100dip, 100dip)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
   ToggleButton1.Checked=Not(ToggleButton1.Checked)
End Sub

Clicking Button1 toggles the state of ToggleButton1.

If you can't get it to work then upload your code to this thread - use the File menu Export as Zip option.

Martin.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

Got it to work.

I had to put

B4X:
Dim ToggleButton1 As ToggleButton 
ToggleButton1.Initialize("MyEventName")

into the Globals Sub and it all worked.

Thanks everyone for your help
 
Upvote 0

cooperlegend

Active Member
Licensed User
Longtime User
I have the same issue here.

I restore the value of the toggle button (active or not)

If lstActive.Get(Position) = 0 Then
togActive.Checked = True
Else
togActive.Checked = False
End If

But the text shown on the toggle button does not update

Putting in Globals did not work
 
Upvote 0

cooperlegend

Active Member
Licensed User
Longtime User
Sorted,

Problem was with

If lstActive.Get(Position) = 0

I had to store this value before doing the comparison against 0
 
Upvote 0
Top