Android Question [Solved] How do I format a numeric EDITEXT?

Sergio Castellari

Active Member
Licensed User
Situation:
1) I need to format an EDITEXT with a numeric mask like "999999.99"
In other words, I need to validate a numerical entry with two decimal places.

2) To be able to change the color of the number if it is positive or negative while writing

Greetings
 

William Lancee

Well-Known Member
Licensed User
Longtime User
If you want to be more adventurous use B4XFloatView which has Masks. But for EditText (Android only) use the TextChanged event.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    EditText1.RequestFocus
End Sub

Private Sub EditText1_TextChanged (Old As String, New As String)
    'Note: In designer, I set EditText1 to expect DECIMAL input.  On numeric keyboards the minus sign requires two clicks on my Samsung device
    If New = "-" Then EditText1.TextColor = xui.Color_Red
    Dim k As  Int = New.IndexOf(".")
    If k > -1 Then
        If New.Length - k > 3 Then
            EditText1.Text = Old
            EditText1.SelectionStart = Old.length
        End If
    End If
End Sub

Private Sub EditText1_EnterPressed
    Log(EditText1.Text)
    EditText1.Text = ""
    EditText1.TextColor = xui.Color_Black
End Sub
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
Brilliant!!! It worked!!!
I had no idea about the "Textchanged" event of the editext.
I also don't understand how New.IndexOf (".") Works.
The "EnterPressed" event never got executed, I imagine it would be for B4J.
Thank you!

NOTE: My knowledge is in the basement! But I'll keep trying!
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
Thank you!
I believe at first to see something read.
Also purchase the Spanish version of the "Penny Press" manual written by Wyken Seagrave.
Great, but it's for B4A version 8.80. Unfortunately they have not updated it
Greetings!
 
Upvote 0
Top