Android Question Tabstrip + Scrollview + Keyboard = Height?

JohnC

Expert
Licensed User
Longtime User
I am using a Tabstrip and a few of the tab's pages have edittext controls on them.

So, I am forced to use a scrollview on each tabstrip page that has a edittext on it to make sure they can be visible when the keyboard is shown.

So, then I am forced to use IME to detect when the keyboard switches show/hide states so I can adjust the viewport size of the scrollview.

But I am having a hard time figuring out how to set the sv.height to the proper height of the visible part of the current tab page.

I would like the tab titles to remain visible, so I need to figure out the height of the visible space between the bottom of the tab titles row and the top of the keyboard.

Any ideas?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
See the attached example.

The important code is:
B4X:
Sub EditText_FocusChanged (HasFocus As Boolean)
   If HasFocus Then
       FocusedField = Sender
       UpdateLayoutIfNeeded
   Else
       Dim FocusedField As EditText 'it will be uninitialized
   End If
End Sub

Private Sub ime_HeightChanged (NewHeight As Int, OldHeight As Int)
   If NewHeight < OldHeight Then
       KeyboardSize = OldHeight - NewHeight
   Else
       KeyboardSize = 0
   End If
   UpdateLayoutIfNeeded
End Sub

Private Sub UpdateLayoutIfNeeded
   If FocusedField.IsInitialized = False Then Return
   Dim LowestVisiblePointRelativeToSV As Int = 100%y - KeyboardSize - 50dip '50dip = tab height
   If FocusedField.IsInitialized Then
       Dim fieldBottom As Int = FocusedField.Top + FocusedField.Height + 2dip
       Dim p As Panel = FocusedField.Parent
       Dim sv As ScrollView = p.Parent
       sv.ScrollPosition = fieldBottom - LowestVisiblePointRelativeToSV
   End If
End Sub

Note that the event name of all EditTexts is EditText.
 

Attachments

  • TabStripWithScrollViews.zip
    11 KB · Views: 696
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Hi,

It seems like the OS will automatically scroll the sv to make sure the focused edittext is visible to the user, so I took a slightly different approach in a solution:

I first created a routine that will search for a view's parent scroll view. This way I didn't need a bunch of DIM's for all the different scrollviews in order to be able to work with just one of them.

B4X:
Sub GetParentScrollView (V As View) As Object
    'returns the scrollview that is parent to the passed view (or returns non scrollview object if not found/error)
    'this allows accessing a particular scrollview from among many contained in an activity without requiring a unique declaration for each scrollview
 
    Dim FoundIt As Boolean = False
 
    Do While FoundIt = False
     
        Dim P As Object
     
        Try
            P = V.parent
        Catch
            Return P
        End Try
 
        If P Is ScrollView Then
            FoundIt = True
        Else
            V = P
        End If

    Loop
 
    Return P
 
End Sub

Then I have this code in the keyboard event:

B4X:
Sub IME_HeightChanged(NewHeight As Int, OldHeight As Int)
 
    Dim R As Object
 
    Select Case tabIcon.CurrentPage
        Case 0
            R = Misc.GetParentScrollView(txtName)
        Case 2
            R = Misc.GetParentScrollView(txtTextMessage)
    End Select
 
    If R Is ScrollView Then
        sv = R
        sv.Height = NewHeight - ActionBar.Height - 50dip 'tab header height
    End If
 
End Sub

I was actually surprised to see that you also used a constant of '50' for the tab height when calculating the scrollview height because there is no "tabMain.height" property to use in a calculation. My thought was that using a fixed value would not be a good value to depend on because it could vary from device to device. But I guess it's not.
 
Upvote 0
Top