Android Question CheckBox.Enabled does not work

Sergey_New

Well-Known Member
Licensed User
Longtime User
He is always available. Is this how it should be?
I solved it with the following code:
B4X:
Sub ch_CheckedChange(Checked As Boolean)
     ch.Checked=True
End Sub
 
Solution
My dialog is created almost the same as in your example. There is no result. I'll create a test example a little later.
Here is a complete working example Sergey. If you want the zip I can attach it, but it is simple and self explanatory:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Dialog As B4XDialog
    Private Ch As CheckBox
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")  'has button1    
End Sub

Sub Button1_Click
    Dialog.Initialize(Root)
    Dim p As B4XView = xui.CreatePanel("")...

JohnC

Expert
Licensed User
Longtime User
If I understand your issue correctly, the CheckBox.Enabled property only effects if the "user" can change the state of it with a touch - and if its false, then the checkbox will be dim in color to give the user a visual cue that they can't change it.

However, even if the .Enabled = False, your "code" can still change the state of the checkbox.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I need the user to not be able to change the state of the CheckBox.
Then if you sent .Enabled = False, the user should not be able to change the state.

When you do set Enabled = False, does the checkbox turn dim in brightness?

If not, what checkbox are you using - is it the default checkbox or a customview?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If I understand you correctly, When you load the layout that has the checkbox and if you want the checkbox to remain true regardless of a click or not, you can simply:
B4X:
Ch.Checked = True
    Ch.Enabled =False
without needing the ch_CheckedChange event
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
What can be done in this case?
Again, you do not provide code or enough info. I am assuming you are using Custom dialog:
B4X:
Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 500dip, 500dip)
    p.LoadLayout("MyDialog3")   'has your checkbox among others
    Dim rs As ResumableSub = Dialog.ShowCustom(p, "OK", "", "CANCEL")   'Dialog is B4XDialog
    Ch.Checked =True ' user cannot change it. If it is set to false it stays false even if user clicks it
    Ch.Enabled =False
    Wait For (rs) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Log(Ch.Checked)
    End If
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
My dialog is created almost the same as in your example. There is no result. I'll create a test example a little later.
Here is a complete working example Sergey. If you want the zip I can attach it, but it is simple and self explanatory:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Dialog As B4XDialog
    Private Ch As CheckBox
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")  'has button1    
End Sub

Sub Button1_Click
    Dialog.Initialize(Root)
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 500dip, 500dip)
    p.LoadLayout("MyDialog3")   'has your checkbox
    p.SetColorAndBorder(xui.Color_Cyan, 6dip, xui.Color_Black, 10dip)
    Dim rs As ResumableSub = Dialog.ShowCustom(p, "OK", "", "CANCEL")
    
    Ch.Color = xui.Color_White
    Ch.Checked =False ' user cannot change it. If it is set to false it stays false even if user clicks it
    Ch.Enabled =False
    Wait For (rs) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Log(Ch.Checked)
    End If
End Sub
 
Upvote 0
Solution

Sergey_New

Well-Known Member
Licensed User
Longtime User
Here is a complete working example
Your example works correctly.
I don't understand why this is not the case in my application.
I will look for the reason, but that will be another topic. Thank you very much for your help and patience!
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
@Sergey_New

I think I see your problem, you do not understand the Checkbox_CheckedChange event completely.

Within the CheckChanged event you can read the state of the CheckBox True or False with (Checked As Boolean). However, there are TWO occasions this event fires:

1. When the user clicks on the view
2. When any code, inside or outside the event, changes the true/false value of Checkbox.

So, if you change the Checkbox value within that event, it will fire twice, once for the user click, and once for the programmatic change.

The attached B4J code demonstrates this as normal behaviour. If you MUST change the value of a Checkbox from within a CheckChanged event, for instance setting up a new page, then you need to set a flag variable to mark when your programmatic setup of your views is complete. Otherwise the event is firing all the time and the process becomes extremely confusing!
 

Attachments

  • Check Box Event Demo.zip
    2 KB · Views: 35
Upvote 0
Top