Android Question B4XFloatTextField TextChanged Validation in a Custom Dialog

Mahares

Expert
Licensed User
Longtime User
The below crashes at Line 67 which is: IsDialogValid(New, txtLastName.Text)
java.lang.RuntimeException: Class instance was not initialized (b4xfloattextfield)
B4X:
Sub Globals
    Private txtFirstName As B4XFloatTextField
    Private txtLastName As B4XFloatTextField
    Private Dialog As B4XDialog
    Private xui As XUI
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")   ''has only button1
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Button1_Click
    Dialog.Initialize(Activity)
    Dialog.Title = "First and last Name"
  
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 300dip, 200dip)
    p.LoadLayout("MyDialog")   'has both B4XFloatTextField B4Xviews
    Dialog.PutAtTop = True
    Dim rs As ResumableSub = Dialog.ShowCustom(p, "OK", "", "CANCEL")
    txtFirstName.TextField.RequestFocus
    IsDialogValid(txtFirstName.Text, txtLastName.text)
    Wait For (rs) Complete (Result As Int)
    Dialog.PutAtTop = False
    If Result = xui.DialogResponse_Positive Then
        Dialog.Show($"Je m'appelle ${txtFirstName.Text} ${txtLastName.Text}"$, "OK", "", "")
    End If
End Sub

Private Sub IsDialogValid (FirstName As String, LastName As String)
    Dim ok As B4XView = Dialog.GetButton(xui.DialogResponse_Positive)
    If FirstName.Length > 0 And LastName.Length > 0 Then
        ok.Enabled = True
    Else
        ok.Enabled = False
    End If
End Sub

Sub txtFirstName_TextChanged (Old As String, New As String)
    IsDialogValid(New, txtLastName.Text)
End Sub

Sub txtLastName_TextChanged (Old As String, New As String)
    IsDialogValid(txtFirstName.Text, New)
End Sub

I am attaching the full project:
 

Attachments

  • B4XCustomDialog020720.zip
    10.7 KB · Views: 225
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. It will be simpler to use B4XPrefencesDialog in this case.

2. The problem is that TextChanged event is raised right after the layout is loaded and before Dialog.ShowCustom is called.

Solution:
B4X:
Sub txtFirstName_TextChanged (Old As String, New As String)
    If Dialog.Visible = False Then Return
    IsDialogValid(New, txtLastName.Text)
End Sub

Sub txtLastName_TextChanged (Old As String, New As String)
    If Dialog.Visible = False Then Return
    IsDialogValid(txtFirstName.Text, New)
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
That works well thank you. I have added a routine to capitalize the first letter of each of the 2 prompts (first and last name) after all the input, but is there a way to automatically force the first letter only to be UPPER case as the data is being typed, maybe using RegEx or some other way. It is still related to this thread.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is still related to this thread
Not really. Questions should only be posted in the first post.

 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Not really. Questions should only be posted in the first post.
The Class you referred me to is not applicable to B4XFloatTextField, so I used its Texfield member as follows to make it work:
B4X:
InpTyp.SetInputType(txtFirstName.Textfield,Array As Int(InpTyp.TYPE_CLASS_TEXT,InpTyp.TYPE_TEXT_FLAG_CAP_SENTENCES))
All in all, it may not be worth it as it adds overhead to the project with the addition of the SLInpTypeConst Class and the Reflection library. I thought maybe the B4XDialog lib has it built-in.
 
Upvote 0
Top