Android Question how to limit the user to type no more than one dot ( . ) on a edittext box view

MiguelCZ

Member
Licensed User
Longtime User
Hi All, this is my first post here an also my first android app ( first overall program ), a simple calculator.
I want to limit the user to type no more than one dot, I do not know how to do it. If you look at the image I uploaded you can see that I can type as many as I want.

this is the simple code I have for that button

B4X:
Sub Button11_Click
  
    Vibrate.Vibrate(30)
  
    EditText1.Text = EditText1.Text & "."
  
End Sub

Thanks.
 

Attachments

  • calc.jpg
    calc.jpg
    37.3 KB · Views: 344
Last edited:

MiguelCZ

Member
Licensed User
Longtime User
This example from Klaus' guide, I had used before.

B4X:
AddPeriod
      If cboRName.SelectedItem.Length >0 Then
        If SelectQTY_or_LotNo= 1 AND LayOutDefault= True Then
            If txtQty.text.Length > 0 Then
                    If txtQty.text = Round(txtQty.text) Then 'checks if the user already pressed period or 0 is the first char
                            writeNumber(".")
                    End If
            Else
                    writeNumber("0") 'adds 0 if it is the first char
                    writeNumber (".")
            End If
        End If
  End If    
End Sub


and filter use alike this
B4X:
Dim IME As IME
        IME.Initialize("IME")
           IME2SetCustomFilter(send,send.INPUT_TYPE_NUMBERS, "0123456789.")


What library you have to use for this code?
 
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
INPUT_TYPE_DECIMAL_NUMBERS should be enough!

Can you post your project? (or that part)

I already fixed the problem using this code

B4X:
Sub Button11_Click
 
    Vibrate.Vibrate(30)
 
    If EditText1.Text.Contains(".") Then
    EditText1.Text = EditText1.Text
    Else
    EditText1.Text = EditText1.Text & "."
    End If
 
End Sub

I just wanted to try the others method
 
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
Ok, but strangely INPUT_TYPE_DECIMAL_NUMBERS does not work for you.

However, you can avoid:
B4X:
EditText1.Text = EditText1.Text

B4X:
    If Not(EditText1.Text.Contains(".")) Then
    EditText1.Text = EditText1.Text & "."
End If

Thanks
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Migual ... here is a trimmed down code to stop users typing multiple decimals "."

in Globals place
B4X:
Dim FilterFlag As Boolean = False

then ...
B4X:
Sub EditText1_TextChanged (Old As String, New As String)
  
   If FilterFlag = False Then
     If New.Contains(".") Then
      
       FilterFlag = True
      
       Dim im As IME
         im.Initialize("")
         im.SetCustomFilter(EditText1, EditText1.INPUT_TYPE_DECIMAL_NUMBERS , "0123456789")
    
     End If
   End If
  
End Sub

This requires IME Library .. For library and install instructions Click Here ...
once installed make sure to include "IME Library" in your project libraries tab
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
This code will work and requires no libs. Be sure to change all four references of et_ to your EditText name.

B4X:
Sub et_TextChanged(Old As String, New As String)
    If Old.Length = New.Length Then Return
    If Old <> "" AND Old.IndexOf(".") > -1 Then
        If New.LastIndexOf(".") > Old.LastIndexOf(".") Then
            New = Old
            et.Text = New : If New.Length > 0 Then et.SelectionStart = New.Length Else et.SelectionStart = 0
        End If
    End If
End Sub
 
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
This code will work and requires no libs. Be sure to change all four references of et_ to your EditText name.

B4X:
Sub et_TextChanged(Old As String, New As String)
    If Old.Length = New.Length Then Return
    If Old <> "" AND Old.IndexOf(".") > -1 Then
        If New.LastIndexOf(".") > Old.LastIndexOf(".") Then
            New = Old
            et.Text = New : If New.Length > 0 Then et.SelectionStart = New.Length Else et.SelectionStart = 0
        End If
    End If
End Sub

Thanks, worked
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Hello,
Sorry for not getting back to you, been busy. Anyway, I'm pleased that you managed to get it working. For yourself and others to look at, I will still add the regular expression code that I used for checking dots in my calculator. The Regex just checks the current display for dots and if there is a dot already there, it escapes out of the routine.

Regular expressions are extremely useful if you know how to use them.

B4X:
    Dim Input As String = "123.45" 'What is currently on the display
    Dim Match As Matcher = Regex.Matcher2("[\.]", Regex.CASE_INSENSITIVE, Input) 'Check if dot(.) has already been used
    If Match.Find = True Then
        Log(Match.Match)
        Return 'Do not allow another dot to be added.
    Else
        'Call your add digit routine here
    End If

Anyway, I like your buttons :)
 
Last edited:
Upvote 0
Top