Android Question RequestFocus issue when using barcode scanner

Andrew King

Member
Licensed User
I am programming an inventory taking app that has 2 edittext input fields: barcode and quantity.
I am not using the soft keyboard as I am using my own UI keyboard made of buttons for the numbers as for my needs.
After entering in the barcode and pressing enter the program should focus the quantity edittext.

Here is the Issue:
When I key in the barcode using self created keys, upon hitting my self created enter key the next code line is to request focus to the quantity edittext. This works fine this way.
However, when I scan the barcode, instead of the focus going to the quantity edittext as intended, the focus goes to a random button.

The only difference in the code is that when keying in the numbers the enter button calls a Sub to requestfocus whereas when scanning I use the EnterPressed event of the Barcode editText to call that same sub.

The barcode scanner is programmed to append a CR at the end of each scan. I have checked to make sure it is also not sending TAB.

I've seen similar posts about this issue but still don't quite understand how the IME library would solve this.

I'm unsure how to solve this issue, any feedback would be greatly appreciated.
 
Last edited:

Andrew King

Member
Licensed User
Ok so I see the IME_HandleAction event is called when an CRLF character is sent to the edittext. Instead of using EditText_EnterPressed to request focus I use the IME_HandleAction. It still has the same result. A random button will get the focus instead of the next edit text I am requesting.

Edit: So I see that I needed to add return true. This keeps the focus on the same edit text. I want to move the focus to the next edit text.
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
it's relatively easy to force the focus from a given view to a specific view. i've tested with 4 edittext views. when i finish entering some text in edittext1 and press the enter/return key, i can shift the focus to, eg, edittext4 instead of edittext2, which might be the logical "next" view. this sounds like what you are trying to do. i haven't tested with my barcode scanner, but i'm optimistic. in the meantime, post a screen capture of your layout to give me an idea of what i'm looking at.
 
Upvote 0

Andrew King

Member
Licensed User
It is relatively easy without a barcode scanner indeed. But with a barcode scanner things start going haywire.

I know that the barcode scanner sends a CRLF at the end of the read. This calls the enterpressed function. I don't understand what is going on that the focus gets redirected to the button even though you have code that requests focus to the edittext2 in the enterpressed function.

I've attached a screenshot of an example where there are two edittexts. The goal is to scan a barcode in one edittext and have the focus shift to the next edittext. So far I've been unable to do it using requestfocus and the IME library.
 

Attachments

  • Capture.PNG
    78.9 KB · Views: 3
Upvote 0

Andrew King

Member
Licensed User
code:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
   
End Sub

Sub Globals
    Private IME As IME
   
    'These global variables will be redeclared each time the activity is created.
    Private EditText2 As EditText
    Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    IME.Initialize("IME")
    IME.AddHandleActionEvent(EditText1)
End Sub

Sub Activity_Resume

End Su

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Sub IME_HandleAction As Boolean
    Dim e As EditText
    e = Sender
    'the code below can be exchanged for the line in EditText1_EnterPressed comment or uncomment
    EditText1.requestfocus
    Return True 'if true this results in the focus remaining on the edittext1. If false, this results in button1 receiving focus
End Sub

Private Sub EditText1_EnterPressed
    'the code below can be exchanged for the line in IME_Handleaction comment or uncomment
    'EditText2.RequestFocus
   
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…