' ScrollView and EditTexts -- Autofocus issue
' Solution to keyboard overlaying EditText views
' Use IME keyboard hanlder - IME_HeightChanged sub
Sub Process_Globals
End Sub
Sub Globals
Dim sv As ScrollView
Dim ed1, ed2, ed3 As EditText
Dim IME_Handler As IME
Dim LastFocusedEditText As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
sv.Initialize(220%y)
sv.Color=Colors.Red
Activity.AddView(sv,0,0,100%x,100%y)
ed1.Initialize("EditText") : ed1.Text="Edit One"
ed2.Initialize("EditText") : ed2.Text="Edit Two"
ed3.Initialize("EditText") : ed3.Text="Edit Three"
sv.Panel.AddView(ed1,10%x,50%y,48%x,12%y)
sv.Panel.AddView(ed2,20%x,90%y,48%x,20%y)
sv.Panel.AddView(ed3,30%x,140%y,48%x,8%y)
' Initialize the IME keyboard handler
' Want to handle height changes so we can monitor when
' soft keyboard pops up
IME_Handler.Initialize("IME")
IME_Handler.AddHeightChangedEvent
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
' When EditText's exist in a ScrollView the FocusChanged sub is called
' by Android in an automated fashion, or when the user clicks the EditText
' Let's grab the last focused EditText
Sub EditText_FocusChanged (HasFocus As Boolean)
If HasFocus=True Then LastFocusedEditText=Sender
End Sub
' When the soft keyboard pops up check to see if the last focused EditText is undermneath it.
Sub IME_HeightChanged(NewHeight As Int, OldHeight As Int)
' Act only when keyboard is opened
If NewHeight<OldHeight Then
If LastFocusedEditText.IsInitialized=True Then
Dim svOffs As Int : svOffs=NewHeight-LastFocusedEditText.Height-30dip
Dim etScreenYPos As Int : etScreenYPos=LastFocusedEditText.Top-sv.ScrollPosition
If etScreenYPos>svOffs Then
sv.ScrollPosition=sv.ScrollPosition+etScreenYPos-svOffs
End If
End If
End If
End Sub