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
 

plager

Member
I tried Sleep() function in many ways. Not always helped. Maybe it has something in common with cover screen on Samsung. Not sure about that. However, ForceHideKeyboard did always good job to me, even on cover screen, also sometimes with combination with Sleep() function. I didn't attach a Sleep() in that function. It may be not that crucial for functionality. It can be implemented as you did - call Sleep() first, then ForceHideKeyboard. Need to try in some combinations.
 
Last edited:

T201016

Active Member
Licensed User
Longtime User
I tried Sleep() function in many ways. Not always helped. Maybe it has something in common with cover screen on Samsung. Not sure about that. However, ForceHideKeyboard did always good job to me, even on cover screen, also sometimes with combination with Sleep() function. I didn't attach a Sleep() in that function. It may be not that crucial for functionality. It can be implemented as you did - call Sleep() first, then ForceHideKeyboard. Need to try in some combinations.
In my opinion, I think the virtual keyboard is more to blame for the problem discussed here than the screen of any brand of phone.

I find the following example safer to use because it happened to me that
pressing a button (in the dialog box) other than Cancel caused the keyboard view to remain active(?!).
I consider this to be at least a strange case, because after selecting and pressing a button, the dialog box itself usually closes.

'Use::
Wait For (ForceHideKeyboard) Complete (force As Boolean)

Sub ForceHideKeyboard As ResumableSub
    Sleep(100)
  
    Dim jo As JavaObject
    jo.InitializeContext

    Dim im As JavaObject = jo.RunMethod("getSystemService", Array("input_method"))
    Dim decor As JavaObject = jo.RunMethodJO("getWindow", Null).RunMethodJO("getDecorView", Null)
    im.RunMethod("hideSoftInputFromWindow", Array(decor.RunMethod("getWindowToken", Null), 0))
    Return True
End Sub

PS. it is worth adding the line in the Manifest: SetActivityAttribute(main, android:windowSoftInputMode, adjustResize|stateHidden)
 
Last edited:
Top