Android Question How to Detect Keyboard Hide Event when Keyboard is Shown and Back Button is Pressed

jjrmh

New Member
Hi All,

Just as the title suggests, I want to know how to detect a keyboard hide event when you did not press the DONE or ENTER key but instead tapped the BACK button (or swiped BACK) to hide the keyboard.

Specifically, I want to resize a custom list view into the available space once the soft keyboard shows up. I am using IME library and yes it can resize the keyboard if I call a Height Changed event when EditText Focus Changed Event is called. It can also hide the keyboard when the DONE or ENTER key is pressed. But when I hide the keyboard using BACK Key, the CLV size remains compressed. I tried to call a height changed event on BACK key press but it can only detect a back key press event when the soft keyboard is not on screen.
 

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Did you try this:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
    'Your code'
    End If
End Sub
 
Upvote 0

jjrmh

New Member
1. You should use IME library for this.
2. You should never call the height changed event yourself.
3. You missed something. Maybe the line that needs to be added in the manifest.

Hi Erel,

First of all, I am really new at B4A so pls bear with me :)
I tried to not manually call the IME Height Changed event (which is the default method) but can't get to resize the view. I get to compress my custom list view only if I manually call the height changed event at the focus changed event on an EditText field on the CLV. Please see my Activity_Create code below:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim pm As B4XPagesManager  
    pm.Initialize(Activity)
    Activity.LoadLayout("UpperMenu")
    pnlResult.Visible = False
    lblResult.Visible = False
    btnOK.Visible = False
    pnlClearList.Visible = False
    lblClearPrompt.Visible = False
    btnClrYes.Visible = False
    btnClrNo.Visible = False
    pnlConfirmSave.Visible = False
    lblConfirmSave.Visible = False
    btnSaveYes.Visible = False
    btnSaveNo.Visible = False
    Panel3.Enabled = False
    Panel3.Visible = False
    ivLogo.Bitmap = LoadBitmap(File.DirAssets, "randomizer icon.png")
   
    IME1.Initialize("ime")
    IME1.AddHeightChangedEvent
    IME1_HeightChanged(DefaultHeight, 0)
   
    xui.SetDataFolder("kvs")
    kvs.Initialize(xui.DefaultFolder, "kvs.dat")
    Dim length As Int = kvs.GetDefault("ListLength", 0)
   
    If length > 0 Then
        For i = 0 To length
            Dim iv As ItemValue
            iv.Initialize
            CLV.Add(CreateItem(iv), iv)
            iv.EditText1.Text = kvs.Get("Item" & i)
            new = False
        Next
    Else
        Dim iv As ItemValue
        iv.Initialize
        CLV.Add(CreateItem(iv), iv)
    End If
End Sub

Below is my manifest editor code:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)

SetActivityAttribute(main, android:windowSoftInputMode, adjustResize|stateHidden)
'SetActivityAttribute(main, android:windowSoftInputMode, adjustPan|stateHidden)
'End of default text.

I also had to set the New Height manually as I can't seem to capture the new actual distance of the top of the keyboard from top of screen. This is what I put on the focus changed, enter pressed, and the height changed event sub itself:

B4X:
Sub EditText1_FocusChanged (HasFocus As Boolean)
    Dim TextBox As EditText = Sender
'    IME1.AddHeightChangedEvent
    If HasFocus And Not(new) Then
        IME1_HeightChanged((7 * TextBox.Height), 0)
        IME1.ShowKeyboard(TextBox)
    Else If HasFocus And new Then
        new = False
        IME1_HeightChanged(DefaultHeight, 0)
        IME1.HideKeyboard
    Else
        IME1_HeightChanged(DefaultHeight, 0)
        IME1.HideKeyboard
    End If
   
End Sub

Sub EditText1_EnterPressed
'    IME1.AddHeightChangedEvent
    IME1_HeightChanged(DefaultHeight, 0)
    IME1.HideKeyboard
End Sub

Sub IME1_HeightChanged(NewHeight As Int, OldHeight As Int)
    CLV.Base_Resize(100%x, NewHeight)
End Sub

Finally, below is my keypress event sub:

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        ToastMessageShow("Swipe Back Again to Exit", False)
        Timer1.Initialize("Timer1", 2000)
        Timer1.Enabled = True
'        IME1.AddHeightChangedEvent
        IME1_HeightChanged(DefaultHeight, 0)
        IME1.HideKeyboard
       
        BackCount = BackCount + 1
        If BackCount = 2 Then
            BackCount = 0
            ExitApplication
        End If
        Return True
        Else
            Return False
    End If
   
    'Return B4XPages.Delegate.Activity_KeyPress(KeyCode)  
End Sub

My main issue is that the layout remains compressed if I did not press the enter button on the keyboard but instead pressed the back button to hide my keyboard. However, I might be using the IME height changed event incorrectly. But like I said, the height changed event cannot be called automatically on my code if I remove the manual height changed call on the focus changed event. Please help me if you ever found something wrong, that will be appreciated.
 
Last edited:
Upvote 0
Top