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:

Peter Simpson

Expert
Licensed User
Longtime User
Hello,
Try this

B4X:
Dim EditText1 As EditText
    EditText1.Initialize(Null)
    EditText1.Text = "012345.6789"
If EditText1.Text.Contains(".") Then ToastMessageShow("I found ....... so do not add it again :-)", False)

The above code should work for you.
 
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
B4X:
Dim EditText1 As EditText
    EditText1.Initialize(Null)
    EditText1.Text = "012345.6789"
If EditText1.Text.Contains(".") Then ToastMessageShow("I found ....... so do not add it again :-)", False)

The above code should work for you.[/quote]

B4X:
EditText1.InputType = EditText1.INPUT_TYPE_DECIMAL_NUMBERS

wow, thank you so much guys.



thanks,
edit, not working
 
Last edited:
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
Hello,
Try this

B4X:
Dim EditText1 As EditText
    EditText1.Initialize(Null)
    EditText1.Text = "012345.6789"
If EditText1.Text.Contains(".") Then ToastMessageShow("I found ....... so do not add it again :-)", False)

The above code should work for you.


looking at it again. using this code, as soon as I press the "." button it will not let me type anything else. not even Clear button

B4X:
Sub Button11_Click
  
    Vibrate.Vibrate(30)
  
    EditText1.Text = EditText1.Text & "."
  
    EditText1.Initialize(Null)
    EditText1.Text = "012345.6789"
  
    If EditText1.Text.Contains(".") Then
    ToastMessageShow("blabla", False)
    End If
  
End Sub
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
You could always check for a dot press before hand, when the dot is pressed it sets a flag(Boolean) to True so that you know that a dot has been used, thus it can't be used again until the C button is presses.

Or you can always use a regular expression RegEx. use [.] as the pattern. The dot is a special character in a Regular Expressions, so you would have to escape the character first...
 
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
You can use something like this:

B4X:
Sub EditText1_TextChanged (Old As String, New As String)
    If Old.Contains(".") Then
        If Asc(New.SubString(New.Length-1)) = Asc(".") Then
            EditText1.Text = Old
            EditText1.SelectionStart = Old.Length
        End If
    End If
End Sub

using this code, for example if I type 2.2 then it works it wil not let me type another ".".
but if I type 2. and then I press "." again it will crash the app, see attached file


thanks, will read through that
 

Attachments

  • calc1.jpg
    calc1.jpg
    42.8 KB · Views: 255
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
Well I think I solved the problem , I used this code and it worked


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

Thank you guys for helping me
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I agree Lucas's code does cause a crash if you type 'consecutive' decimal points. trying alternatives as we speak

I don't see how your code above stops users typing multiple "."
to me it just makes sure the text DOES contain a decimal point ?
 
Upvote 0

Theera

Expert
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.")
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
this will allow users to type a single Decimal point then allow ONLY 0-9 entries.. (Disables the decimal key)

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
       FilterEntrys
     End If
   End If

End Sub

Sub FilterEntrys

  Dim im As IME
  Dim sb As StringBuilder
  sb.Initialize

  For i = 48 To 57
  sb.Append(Chr(i)) '0 - 9
  Next

  im.SetCustomFilter(EditText1, Bit.OR(EditText1.INPUT_TYPE_DECIMAL_NUMBERS,524288) , sb.ToString) '524288 - no suggestions

End Sub
 
Last edited:
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
this will allow users to type a single Decimal point then allow ONLY 0-9 entries.. (Disables the decimal key)

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
       FilterEntrys
     End If
   End If

End Sub

Sub FilterEntrys

  Dim im As IME
  Dim sb As StringBuilder
  sb.Initialize

  For i = 48 To 57
  sb.Append(Chr(i)) '0 - 9
  Next

  im.SetCustomFilter(EditText1, Bit.OR(EditText1.INPUT_TYPE_DECIMAL_NUMBERS,524288) , sb.ToString) '524288 - no suggestions

End Sub


leaving the "." button like this,

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

and using your code, is not working, do I have to add something else?
 
Upvote 0
Top