Android Question Edittext Mask

Edu Chamon

Member
Licensed User
Good night. Please would like to show a mask while the user is typing in editext.
for example, value in Brazil.

1,234,567.89

how to go showing this to the user?

0.01
0.12
1.23
12.34
1,23.45 ........
thank you very much
 

emexes

Expert
Licensed User
In the value change event, try:
B4X:
'remove existing decimal point, leaving string of digits
NewString = NewString.Replace(".", "")

'remove excess leading zeroes
Do While NewString.Length > 3
    If NewString.CharAt(0) <> "0" Then
        Exit
    End If

    NewString = NewString.SubString(1)
Loop

'ensure at least three digits
Do While NewString.Length < 3
    NewString = "0" & NewString
Loop

Dim DigitsBefore As Int = NewString.Length - 2

'insert decimal point
NewString = NewString.SubString2(0, DigitsBefore) & "." & NewString.SubString(DigitsBefore)
 

Edu Chamon

Member
Licensed User
Sorry to continue with this and the question.

I had even looked at the links before in the forum.

At home I test on B4a, here I am trying on B4j.

I used as reference the field Textarea.

In TextArea1_TextChanged event (Old As String, New As String)


In the case of the example above or if I triggered a sub

NewString = Old

.
.
.
.

New = NewString


It would be like this ?

Thank you for understanding
 

emexes

Expert
Licensed User
It would be like this ?
My previous effort was written from memory on a small-screen mobile device during a lull at a wake, so... the odds of it working first-up were low(ish).

The posts indicated by DonManfred look good, but if you still want to roll your own simple version, then...

Try this instead, where EditMaskCashRegister is a general routine that you can use for multiple EditTexts, and TextArea1_TextChanged is an example of its use, using the view name you indicated:
B4X:
Sub TextArea1_TextChanged (Old As String, New As String)
  
    EditMaskCashRegister(TextArea1, Old, New)
  
End Sub
  
'automatically adds/moves decimal point for 0.00 format  
Sub EditMaskCashRegister(ViewHandle As EditText, Old As String, New As String)
  
    'remove existing decimal point, leaving string of digits
    Dim Temp As String = New.Replace(".", "")
  
    'remove excess leading zeroes
    Do While Temp.Length > 3
        If Temp.CharAt(0) = "0" Then
            Temp = Temp.SubString(1)
        Else
            Exit
        End If
    Loop
  
    'ensure at least three digits
    Do While Temp.Length < 3
        Temp = "0" & Temp
    Loop
  
    Dim DigitsBefore As Int = Temp.Length - 2
  
    'insert decimal point
    Temp = Temp.SubString2(0, DigitsBefore) & "." & Temp.SubString(DigitsBefore)
  
    If Temp <> New Then
        Log(New & " ==> " & Temp)
      
        'put field back to view
        ViewHandle.Text = Temp

        'postion cursor at end
        ViewHandle.SelectionStart = Temp.Length
    End If
  
End Sub
 
Top