Android Question How to hide keyboard by code?

William Lancee

Well-Known Member
Licensed User
Longtime User
If you want to automatically accept the number after 2 decimal places have been entered, you need to use IME to hide the keyboard. Some audio feedback may be required to let the user know the entry is complete.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private EditText1 As EditText
    Private beep As Beeper            'Audio library
    Private IME1 As IME                'IME library (not IME2)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    beep.Initialize(250, 7040)
    IME1.Initialize("")
    EditText1.RequestFocus
End Sub

Private Sub EditText1_TextChanged (Old As String, New As String)
    'Note: In designer, I set EditText1 to expect DECIMAL input.  On numeric keyboards the minus sign requires two clicks on my Samsung device
    If New.LastIndexOf("-") >0 Then Return        'prevents minus inside number
    If New = "-" Then EditText1.TextColor = xui.Color_Red
    Dim k As  Int = New.IndexOf(".")
    If k > -1 Then
        If New.Length - k = 3 Then         'also prevents multiple periods, auto registers the amount and resets box when 2 decimal places are reached
            beep.beep
            EditText1_EnterPressed
            IME1.HideKeyboard
        End If
    End If
End Sub

Private Sub EditText1_EnterPressed
    Log("Done Pressed " & EditText1.Text)
    EditText1.Text = ""
    EditText1.TextColor = xui.Color_Black
End Sub
 
Upvote 0
Top