Android Question B4X Pages, IME and Application Resume

asmondien

Member
Licensed User
Longtime User
Hello,

Hope you can help - I'm getting back to b4a, and been struggling with b4x pages, and keyboard when closing and opening.

On registration page, I need to push the view top by X, so it's show the input boxes behind the keyboard, I did saw there is an option to use scroll view, but I thought I try the IME from what I saw the tutorial, and other posts.

I used the code from this post : https://www.b4x.com/android/forum/threads/how-to-detect-if-ime-soft-keyboard-is-closed.141183/

B4X:
Sub IME_HeightChanged(NewHeight As Int, OldHeight As Int)
    Private IsIMEKeyboardClosed As Boolean = (NewHeight = ActivityHeight)
    LogColor("IsIMEKeyboardClosed >>   " & IsIMEKeyboardClosed, Colors.Red)
End Sub

My if check -

B4X:
    If IsIMEKeyboardClosed = True Then           
        contentPanel.SetLayoutAnimated(400, contentPanel.Left, OriginalTop, contentPanel.Width, contentPanel.Height)
    Else       
        contentPanel.SetLayoutAnimated(400, contentPanel.Left, OriginalTop - 120dip, contentPanel.Width, contentPanel.Height)
    End If

Which does what it suppose to - I can move the layout to the top and see the input boxes, and keyboard closes and it's back to bottom.

However if I press the lock screen button on phone, and come back - this stop functioning and won't move the layout back to top by X

So, is this the best practice? and What is the solution to make the the IME_HeightChange triggers again after resume?

Thank you.
 
Solution
Hey!

If you are using B4XPages, then you will delegate out the KeyboardStateChanged event from your Main activity using IME.

Meaning -- Whichever page you are working on can have a B4XPage_KeyboardStateChanged(Height as int) definition.

See B4XPages: https://www.b4x.com/android/forum/t...or-managing-multiple-pages.118901/post-745090

Add this to your manifest:
B4X:
SetActivityAttribute(main, android:windowSoftInputMode, adjustResize|stateHidden)

In your main module:
B4X:
Sub Globals
    Private ime As IME
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ime.Initialize("IME")
    ime.AddHeightChangedEvent
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
End Sub...

LucasHeer

Active Member
Licensed User
Longtime User
Hey!

If you are using B4XPages, then you will delegate out the KeyboardStateChanged event from your Main activity using IME.

Meaning -- Whichever page you are working on can have a B4XPage_KeyboardStateChanged(Height as int) definition.

See B4XPages: https://www.b4x.com/android/forum/t...or-managing-multiple-pages.118901/post-745090

Add this to your manifest:
B4X:
SetActivityAttribute(main, android:windowSoftInputMode, adjustResize|stateHidden)

In your main module:
B4X:
Sub Globals
    Private ime As IME
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ime.Initialize("IME")
    ime.AddHeightChangedEvent
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
End Sub

Sub IME_HeightChanged (NewHeight As Int, OldHeight As Int)
    B4XPages.GetManager.RaiseEvent(B4XPages.GetManager.GetTopPage, "IME_HeightChanged", Array(NewHeight, OldHeight))
End Sub

Then, in each page where you need dynamic views based on keyboard height, you add your procedure. Here is an example of where I use it in Page_Login (A B4XPage for B4i/B4a):
B4X:
Private Sub B4XPage_KeyboardStateChanged(Height As Int)
    #if b4i
        If Height > 0 Then 'Keyboard Enter
            loginPanel.Top = Root.Height - Height - loginPanel.Height - 20dip
        Else
            loginPanel.Top = (Root.Height / 2) - (loginPanel.Height /2)
        End If
    #Else If b4a
        loginPanel.Top = (Height / 2) - (loginPanel.Height /2)
    #End If
End Sub

This is great -- because you only need to define once in your main activity and can have your keyboard event throughout all of your pages.
 
Upvote 1
Solution
Top