Android Question Edittext Focus Issues

chrisinky

Member
Licensed User
Longtime User
So odd problem here...

First I generate all my screens on the fly from data sent to me from a server, so everything is drawn at runtime and in order (I've verified by dropping my edittexts to a map with an incrementing # as they are drawn and reading that back).

So - lets say I have a screen with 8 edittext fields on it. I start at the top. I enter my data and on the soft keyboard I hit enter/next whatever that version of android says/shows. I advance to the next field with no issues, I go down all 8 in perfect order.

IF I use a hardware keyboard (on a custom device) or a bluetooth keyboard and I hit the physical enter key on that keyboard I skip to the 3rd field down. If I use the tab key on these same keyboards the focus goes through the perfect desired tab order.

I wrote code that detects the enter pressed event, stores a map of what fields are in what order, and look at itself in that order and will manually setfocus to the proper next edittext. But even with that code (and debug showing it's picking the correct next text box) it skips down from ET1 to ET3, not ET2 like I expect....

If I use the soft keyboard on this same device it all works perfect...

Anyone seen this before?
 

JordiCP

Expert
Licensed User
Longtime User
Perhaps the hardware keyboard is sending a CR+LF pair (so, two keys 0x0D and 0x0A) while the software keyboard only sends one (0x0A = 10d).

If this is the case (not sure, just a guess) you can try something like this example
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim ed(3) As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")

   For k=0 To 2
     ed(k).Initialize("ed")
     ed(k).SingleLine=False     '<-- You can capture any "enter" key in _textChanged event
     ed(k).Text="Hello "& (k+1)
     Activity.AddView(ed(k),20%X,20%Y+k*20%Y,60%X,15%Y)  
   Next

End Sub

Sub  ed_EnterPressed
   'do nothing
End Sub

Sub  ed_TextChanged (Old As String, New As String)
   Dim LF0 As String = Chr(10)
   Dim CR0 As String = Chr(13)   '(=0x0D) carriage return possibly sent by the hardware keyboard along with the LF char (0x0A)
   if New.Contains(CR0) Then
      Log("Carriage Return detected") <-- extra char sent by your hardware keyboard.
      New.Replace(CR0,"")     'Remove unwanted character.
   End If
   If New.EndsWith(LF0) Then     'This should be the 'normal' enter from the soft keyboard
     New.Replace(LF0,"")
     ' Force jump to the edittext that corresponds  
   End If
   
End Sub


(To avoid confusion, the newline character in B4A is named 'CRLF' but it is only the LF char (0x0A = 10d))
 
Upvote 0

chrisinky

Member
Licensed User
Longtime User
Perhaps the hardware keyboard is sending a CR+LF pair (so, two keys 0x0D and 0x0A) while the software keyboard only sends one (0x0A = 10d).

Thanks for the code - I've tried it and it doesn't detect anything crazy going on with the enter key....
 
Upvote 0

chrisinky

Member
Licensed User
Longtime User
You can use the Accessibility library to explicitly set the focus order.
Thanks Erel - I saw that but the docs mention it's just for the DPAD. I tried it regardless and the enter key still skips fields. Using the onscreen keyboard OR the hardware dpad up/down buttons advances the edit text boxes in the proper order....
 
Upvote 0

chrisinky

Member
Licensed User
Longtime User
Figured this might help explain...
Here is a video of the issue.
This is a brand new project, I drew 5 edit text boxes in designer, have it load layout1 and thats it, there is 0 code behind this project.
Note that the up/down buttons and the soft keyboard obey tab order.
The physical enter button does not.
I've also tried:
Bluetooth keyboard (same behavior)
USB OTG adapter to wired keyboard (same behavior)
So the actual enter key almost apears to be sending two enters in B4A...

In my actual application I harcoded the tab order (I capture the enter press event and specifically call the next edit text field). When I do this, you can see the correct/next text box get highlighted for a milisecond, then it skips to one 2 down.....

I just created the same project (5 text boxes) in MIT's app inventor for kicks and giggles. Guess what? Physical keyboards do the same skipping of fields in that app as in B4A while the soft keyboard works!?!?!?!!? Now pulling hair out...
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

chrisinky

Member
Licensed User
Longtime User
Thanks - it must be the keyboard.... using the IME library I'm able to filter out the duplicate keypresses and it works fine now.

But for whatever reason the following devices had this issue with my cheap Bluetooth keyboard and the Pixel with a OTG adapter to a wired usb keyboard:
Pixel XL
S4
S5

Workaround is working though!


I've tested it here with a Bluetooth keyboard and it works properly.

I think that this error is specific to the device you are using. You can handle the action button with IME library: https://www.b4x.com/android/forum/t...-keyboard-with-the-ime-library.14832/#content
Store the time of the last action and ignore actions that happen immediately after the previous one.
 
Upvote 0
Top