Android Question How to predetermine the numeric keyboard in a Dialog?

Sergio Castellari

Active Member
Licensed User
Hello people,

I have the following code that uses a B4XTable to 'load' NUMERIC values in some columns:
B4X:
'Proceso la carga...
   If column.Id = "Energía" Or column.Id = "Agua" Then
       InputTemplate.Text = value
       InputTemplate.lblTitle.Text = column.Id
       Wait For (Dialog.ShowTemplate(InputTemplate, "Aceptar", "", "Cancelar")) Complete (Result As Int)
       If Result = xui.DialogResponse_Positive Then

This code shows on screen a data entry dialog and the 'Full keyboard' ... so if you enter 'letters' it produces an error when recording the information as it expects them to be numbers.

Query:
1) How can I activate ONLY the numeric keypad?
2) or how you can 'control that Result is ONLY a numerical value and otherwise ... put ZERO or ask again

Regards,
Sergio
 

emexes

Expert
Licensed User
I don't know about q1 (although the ide tips-as-you-type auto-help might reveal an InputTemplate property to select the keyboard type).

For q2, you can use IsNumber to check whether a string can be cast/parsed to a number (well, a double - so watch out for large values):
upload_2019-7-12_11-16-43.png

so perhaps something like:
B4X:
Do While True
    InputTemplate.Text = value
    InputTemplate.lblTitle.Text = column.Id
    Wait For (Dialog.ShowTemplate(InputTemplate, "Aceptar", "", "Cancelar")) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        If IsNumber(InputTemplate.Text) Then
            Exit 'Do Loop
        End If
    End If
Loop
which, if the user enters an invalid number, will reset the initial/default entry to value and ask 'em again. I'm not sure what should happen if the user chooses Cancelar, so I've left that for you to think about ;-)
 
Upvote 0

emexes

Expert
Licensed User
A slightly shadier way to check that something is a valid number might be:
B4X:
Try
    MyIntVariable = InputTemplate.Text

Catch
    Log("Nice try, no cigar - that ain't no number")

End Try
but I get a bit nervous intentionally doing stuff that will likely generate an error - feels like I'm inviting trouble into my program.

But it works when I test it, so... what could possibly go wrong?!?!
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
Another way to check for a numeric-only string could be by using regex. You could even check whether the value falls in a specific range.
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
Hello @emexes !!!

Thanks for your help!. Someone passed me a property that directly disables letters and ONLY allows positive numbers:

B4X:
If column.Id = "Energía" Or column.Id = "Agua" Then
       InputTemplate.Text = value
       InputTemplate.lblTitle.Text = column.Id
       InputTemplate.ConfigureForNumbers(False, False) 'AllowDecimals, AllowNegative (Decimales,Negativos)    '<<<<<<<<
       Wait For (Dialog.ShowTemplate(InputTemplate, "Aceptar", "", "Cancelar")) Complete (Result As Int)
       If Result = xui.DialogResponse_Positive Then

With this I work perfect!

Regards,
Sergio
 
Upvote 0
Top