Android Code Snippet Force Hide Keyboard

Been upset lately about soft keyboard not always hides itself when needed, especially on cover screen on Samsung Flip 7. Those callings didn't help and keyboard was still sticking out. Not sure, if it comes from cover screen controlability, or Samsung.
B4X:
phone.HideKeyboard(Main) ' or any other view which had a focus with keyboard
ime.HideKeyboard

For hiding keyboard I had to press 'back' key at the bottom of the screen. Not too comfy for me.
Been creative with some chatBots. One of them suggested this procedure. It's working on A13, and A16. I believe it can work on other android versions as well. Tested on B4A version 13.40. Works flawlessly!

B4X:
Sub ForceHideKeyboard
    Dim jo As JavaObject
    jo.InitializeContext

    Dim imm As JavaObject = jo.RunMethod("getSystemService", Array("input_method"))
    Dim decor As JavaObject = jo.RunMethodJO("getWindow", Null).RunMethodJO("getDecorView", Null)
    imm.RunMethod("hideSoftInputFromWindow", Array(decor.RunMethod("getWindowToken", Null), 0))
End Sub
 
Last edited:

T201016

Active Member
Licensed User
Longtime User
Been upset lately about soft keyboard not always hides itself when needed, especially on cover screen on Samsung Flip 7. Those callings didn't help and keyboard was still sticking out. Not sure, if it comes from cover screen controlability, or Samsung.
B4X:
phone.HideKeyboard(Main) ' or any other view which had a focus with keyboard
ime.HideKeyboard

For hiding keyboard I had to press 'back' key at the bottom of the screen. Not too comfy for me.
Been creative with some chatBots. One of them suggested this procedure. It's working on A13, and A16. I believe it can work on other android versions as well. Tested on B4A version 13.40. Works flawlessly!

B4X:
Sub ForceHideKeyboard
    Dim jo As JavaObject
    jo.InitializeContext

    Dim imm As JavaObject = jo.RunMethod("getSystemService", Array("input_method"))
    Dim decor As JavaObject = jo.RunMethodJO("getWindow", Null).RunMethodJO("getDecorView", Null)
    imm.RunMethod("hideSoftInputFromWindow", Array(decor.RunMethod("getWindowToken", Null), 0))
End Sub
Hello, I'm glad you shared the example on how to close the visible keyboard. The standard way didn't always work properly for me in projects.
So, in my opinion, this is not due to the ability to control the screen from Samsung. I worked on different models and the problem was similar.
 

plager

Member
As I said, usually it works, but sometimes it doesn't, especially on cover screen on Samsung Flip 7. This piece of code was tested on Samsung S20+ with A13, and Samsung Flip 7 FE with A16. It worked on both models flawlessly, and also on cover screen son flip phone.
Glad to help ;)
 

T201016

Active Member
Licensed User
Longtime User
As I said, usually it works, but sometimes it doesn't, especially on cover screen on Samsung Flip 7. This piece of code was tested on Samsung S20+ with A13, and Samsung Flip 7 FE with A16. It worked on both models flawlessly, and also on cover screen son flip phone.
Glad to help ;)
I did a test of your code (ForceHideKeyboard) on my example below,
and unfortunately I have to confirm that the keyboard still remains on the screen.
Only the preceding call of the "Sleep(100)" function allowed it to be hidden.

Similarly, using the two instructions below also hides the keyboard:
Sleep(100)
ime.HideKeyboard


This is an example usage snippet::
    Dim sf As Object = DetailsDialog.ShowAsync("Encrypt binary file?"&CRLF&f.Name, "Encypt now", "Cancel", "Delete", LoadBitmap(File.DirAssets, png&".png"), True)
    DetailsDialog.SetSize(100%x, 450dip)
    Wait For (sf) Dialog_Ready(pnl As Panel)
    pnl.LoadLayout("PDF_EncryptDialog")

    DialogAlgorithm.AddAll(Array As String("MD5","SHA-1","SHA-224","SHA-256","SHA-384","SHA-512"))
                   
    DialogAlgorithm.SelectedIndex = 5
    DialogAlgorithm.Invalidate

    DetailsDialog.GetButton(DialogResponse.POSITIVE).Enabled = False
    Wait For (sf) Dialog_Result(Result As Int)

    DialogPassword.TextField.Enabled = False
    Sleep(100)
    ForceHideKeyboard    '----------- OK!
   
'    or
   
    Sleep(100)
    ime.HideKeyboard    '----------- OK!

Screenshot.jpg
 
Top