Android Question EditText as numeric input, with minimum check

Cableguy

Expert
Licensed User
Longtime User
Hi guys,

I'm implementing an EditText to be a numeric input, however, I also set a Minimum and Maximum values to it, wich I check in the texthaschanged event.
my problem is shown in the log line below:
B4X:
java.lang.NumberFormatException: Invalid double: ""

This happens when I empty(delete) the value in the edittext.
I have tried to check if the new value is a number with IsNumber(New) but it still errors out.
how can I prevent this? should I opt out for a try/catch block?
 

JonPM

Well-Known Member
Licensed User
Longtime User
Have you tried something like this in your texthaschanged event?

B4X:
if edittext.text <> "" then
...
do your stuff
...
end
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I have tried all the above, including testing for "" and for null, it still breaks
 
Last edited:
Upvote 0

KMatle

Expert
Licensed User
Longtime User
First you have to check the length ("Null" can't be checked to be numeric). So the best check is:

B4X:
Sub EditText1_TextChanged (Old As String, New As String)
    If New.Lenght > 0 and IsNumber(New) and New >10 and New <100  Then
       ...
    Else
       Calculate something
    End If
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
So I found a way to make it work s I expect it to do:

B4X:
Private Sub Number_TextChanged (Old As String, New As String)
    Log(New)
    If New.Length > 0 Then
        If New < mMin Or New > mMax Then
            Number.Text = Old
        End If
    End If   
End Sub

private Sub Number_EnterPressed
    If Number.Text.Length>0 And Number.Text <= mMax Then
    Else
        Number.Text = mMin
    End If
End Sub

In TextCanged I check, as suggested, the length of the inputed try(text), if its empty, I just ignore it, and return the text to the previous value. This alone was not enough to prevent the invalid double error, because if I pressed enter with an empty entry, it triggered the error, so I make sure in the EnterPressed event hat, if the text is empty, I revert to the Minimum value.

Thanks for all the suggestions, they putted me on the right track!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
only if the entered value is empty, else it only permits entry in between of min and max values.
note that, for my own simplicity, the first check does nothing and only acts on all other scenarios
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
mMin = 50

I press 7:
B4X:
Private Sub Number_TextChanged (Old As String, New As String)
    Log(New)
    If New.Length > 0 Then '<--- True
        If New < mMin Or New > mMax Then '<--- True but I want to enter 777
            Number.Text = Old
        End If
    End If  
End Sub

on first 7:
Number.Text = Old
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I see your point, but, in my implementation, when setting the minimum value I also set the edittext.text to it ( its only logical, since its the minimum), so the user cannot delete the entered value to insert a new one. example
Minimum is 50
value is 50, user tries to delete it, he cant, so its still 50, but user wants 77
he types 77, this showing 7750, and then deletes the 50
My solution is not perfect, but it suites my current needs.
Still I will try to find another way to accomplish this
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I did some testing, and you were absolutely right!!!
So I re-thought and re-wrote my number checking routine and ended up with a much simpler one, that seems to work much better
B4X:
    If Number.Text.Length>0 Then
        If Number.Text <= mMax And Number.Text>= mMin Then
        Else
            Number.Text = mMin
        End If
    Else
        Number.Text = mMin
    End If

Thank you @LucaMs
 
Upvote 0
Top