Android Question [SOLVED] Disabling OK button in B4X Dialog

Discussion in 'Android Questions' started by JMB, Jan 10, 2019.

  1. JMB

    JMB Active Member Licensed User

    How does one disable the OK button on a B4X dialog?

    I tried using
    Code:
    dialog.GetButton(DialogResponse.POSITIVE).Enabled = False
    This seemed to work before, but doesn't appear to work with the B4X Dialog code.

    JMB
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Make sure to use the latest version of B4XDialog which is included in XUI Views library.

    Your code should work though it is better to use xui.DialogResponse_Positive.
     
  3. JMB

    JMB Active Member Licensed User

    Thanks for that. I am using v1.61 of the XUI Views library.

    Still getting problems with this.

    Here's the code:
    Declaration of the variable:
    Code:
    Private thewifidialog As B4XDialog
    Initialisation of the dialog in Activity_FirstTime
    Code:
    thewifidialog.Initialize(Activity)
    thewifidialog.Title = 
    "Wifi Credentials"
    The Sub that shows the dialog:

    Code:
    Sub btnGetWifiData_Click
     
        
    Dim dialogPanel As B4XView = xui.CreatePanel("")
        dialogPanel.SetLayoutAnimated(
    10000300dip250dip)
        dialogPanel.LoadLayout(
    "wificredentials")
     
        thewifidialog.PutAtTop = 
    True

        
    'Set the default value for the only item on the Layout
        b4xFTF_SSID.Text = myMLWifiObject.WifiSSID
     
        thewifidialog.GetButton(xui.DialogResponse_Positive).Enabled = 
    False
     
        
    Wait For (thewifidialog.ShowCustom(dialogPanel, "OK""""CANCEL")) Complete (Result As Int)

        
    If Result = xui.DialogResponse_Positive Then
            
    Log(b4xFTF_SSID.Text)
        
    Else
            
    Log("Nothing entered.")
        
    End If
    If I comment out
    Code:
    thewifidialog.GetButton(xui.DialogResponse_Positive).Enabled = False
    , the code works. If I include that line I get:
    Code:
    Error occurred on line: 180 (B4XDialog)
    java.lang.RuntimeException: Object should first be initialized (B4XView).
    Interestingly, the Sub shown above is nowhere near line 180 - in fact, line 180 is a comment

    JMB.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    The buttons are only available after you call Dialog.Show. So what can you do as you are calling Wait For???

    Answer:
    Code:
    Dim rs As ResumableSub = thewifidialog.ShowCustom(dialogPanel, "OK""""CANCEL")
     thewifidialog.GetButton(xui.DialogResponse_Positive).Enabled = 
    False
     
    Wait For (rs) Complete (Result As Int)
     
    JMB likes this.
  5. JMB

    JMB Active Member Licensed User

    Thanks for that fix.

    However, it brings up another issue.

    In the B4A dialog code (pre-B4X), a call was made to a function "CheckAllFieldsValid"

    When I try and do this, I get a similar "Object should first be initialized" error.

    I think this is because the views are being created on the resumable sub and firing off the events before the dialog is fully complete - or something like that - and I have fixed it crudely by capturing the error, but I was wondering if there was a more elegant (correct) way of setting the thewifidialog.GetButton(xui.DialogResponse_Positive).Enabled to True.

    Here is the code:

    Code:
    Sub b4xFTF_SSID_TextChanged (Old As String, New As String)
        
    Log("DEBUG: Got to here")
        
    Log("Old is :" & Old)
        
    Log("New is :" & New)
        
    Try
            
    Log(b4xFTF_SSID.Text)
        
    Catch
            
    Log(LastException)
        
    End Try
        
    'CheckAllFieldsValid
    End Sub
    Thanks.

    JMB
     
    Last edited: Jan 11, 2019
  6. Erel

    Erel Administrator Staff Member Licensed User

    Please upload a small project with the dialog.
     
  7. JMB

    JMB Active Member Licensed User

    Here it is...
     

    Attached Files:

  8. Erel

    Erel Administrator Staff Member Licensed User

    It happens because the TextChanged event is fired when the layout is loaded and before the dialog is created. You can add a check for this:
    Code:
    Sub ftfSSID_TextChanged (Old As String, New As String)
       
    If thewifidialog.Visible Then
           thewifidialog.GetButton(xui.DialogResponse_Positive).Enabled = DataIsValid
       
    End If
    End Sub
     
  9. JMB

    JMB Active Member Licensed User

    Thanks Erel.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice