Android Question [SOLVED] Disabling OK button in B4X Dialog

JMB

Active Member
Licensed User
Longtime User
How does one disable the OK button on a B4X dialog?

I tried using
B4X:
dialog.GetButton(DialogResponse.POSITIVE).Enabled = False

This seemed to work before, but doesn't appear to work with the B4X Dialog code.

JMB
 

JMB

Active Member
Licensed User
Longtime 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:
B4X:
    Private thewifidialog As B4XDialog

Initialisation of the dialog in Activity_FirstTime
B4X:
thewifidialog.Initialize(Activity)
thewifidialog.Title = "Wifi Credentials"

The Sub that shows the dialog:

B4X:
Sub btnGetWifiData_Click
 
    Dim dialogPanel As B4XView = xui.CreatePanel("")
    dialogPanel.SetLayoutAnimated(100, 0, 0, 300dip, 250dip)
    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
B4X:
thewifidialog.GetButton(xui.DialogResponse_Positive).Enabled = False
, the code works. If I include that line I get:
B4X:
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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The buttons are only available after you call Dialog.Show. So what can you do as you are calling Wait For???

Answer:
B4X:
Dim rs As ResumableSub = thewifidialog.ShowCustom(dialogPanel, "OK", "", "CANCEL")
 thewifidialog.GetButton(xui.DialogResponse_Positive).Enabled = False
 Wait For (rs) Complete (Result As Int)
 
  • Like
Reactions: JMB
Upvote 0

JMB

Active Member
Licensed User
Longtime 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:

B4X:
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:
Upvote 0

JMB

Active Member
Licensed User
Longtime User
Here it is...
 

Attachments

  • B4A - Dialog Demo for Forum.zip
    444.9 KB · Views: 100
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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:
B4X:
Sub ftfSSID_TextChanged (Old As String, New As String)
   If thewifidialog.Visible Then
       thewifidialog.GetButton(xui.DialogResponse_Positive).Enabled = DataIsValid
   End If
End Sub
 
Upvote 0
Top