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'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.
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
    Capture.PNG
    78.9 KB · Views: 12
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

drgottjr

Expert
Licensed User
Longtime User
i believe it works with my netum scanner. i want to mimic your setup. will revert later
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
that picture is not the layout you described. that's what i want to see. i've already done what you did with a couple views (that's how i know it works). i want to understand what your layout looks like. android uses an algorithm when dealing with a grid of views. that's why you get random focus.
 
Upvote 0

Andrew King

Member
Licensed User
that picture is not the layout you described. that's what i want to see. i've already done what you did with a couple views (that's how i know it works). i want to understand what your layout looks like. android uses an algorithm when dealing with a grid of views. that's why you get random focus.
Ok attached below is the original. I just made a simple b4a project to isolate the problem. For me with the simple project it still is acting goofy. I figured if I could get the simple project to work correctly, I could get my original project to work right.
 

Attachments

  • Capture2.PNG
    Capture2.PNG
    96.2 KB · Views: 9
Upvote 0

Andrew King

Member
Licensed User
that picture is not the layout you described. that's what i want to see. i've already done what you did with a couple views (that's how i know it works). i want to understand what your layout looks like. android uses an algorithm when dealing with a grid of views. that's why you get random focus.
Can you upload the project you got to work with a couple of views? I'd like to test to see if it works for me too. Thanks.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Ok attached below is the original. I just made a simple b4a project to isolate the problem. For me with the simple project it still is acting goofy. I figured if I could get the simple project to work correctly, I could get my original project to work right.
yes, that's what i'm looking for.
the txtInputBarcode edittext is single line, yes? you have that attribute set, yes? is the forceDone attribute set?
when the scanner decodes the barcode and displays it in txtInputBarcode, the next thing that is to happen
always is txtInputQuantity receives the focus? is it an edittext also? if so, why? and why does it receive
the focus (if that's what's supposed to happen)? after the barcode has been scanned, there is no reason for any
view to receive the focus since you have no idea what the user is going to do. so exactly what is happening? and
please clarify why txtinputQuantity would be an edittext.

am i correct in assuming the screen with the edittext views and buttons is on a panel? and when you make the panel
visible, you set the focus to txtInputBarcode? does the softkeyboard pop up any any time?
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
Hi Andrew,
I see you're building a Barcode scanning app, and so I wanted to see if I can help you out with what I built a few years ago and has been in use for that long at multiple company locations.

This demo app was built from code and layouts that I used in the above app. It just covers the scanning part of the app. And so I hope that it can help in some way.
I kept this demo app as fundamental as possible for the demonstration of scanning, entering a quantity, and adding the scanned info into a CustomListView. From there you can add the data to a local database. Then later - as I did - upload that scanned data to the remote server.
In my working company app, they use connected/wired barcode readers that are setup, on the scanner, to send a CR on each scan.
This sends the FocusChanged event to the txtBarcode EditText. The code in that event does the saving work.

See the app in the attached ZIP file.

HTH,
Mark Stuart
 

Attachments

  • BarCodeScanDemo.zip
    12.7 KB · Views: 6
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
attached is how things work for me.

hid and ime keyboards follow a different logic (as you have seen).
having more than 1 edittext is a problem. fortunately, it is not necessary.
both buttons and edittext views can receive focus. when you unset their
focusability, they do not behave in the same way. fortunately, in your
case, we can unset the buttons' focusability. that is what we do.

the barcode is scanned and decoded and output to the edittext view. after
that, there is no reason to switch focus. we simply wait for the user to tap
a quantiy button or a done button. if a quantity button is tapped, its value
appears in a label, not an edittext. a second edittext serves no purpose, since
there is no softkeyboard for input. the buttons serve that purpose, and they
don't need an edittext to display the quantity selected.

once you turn off the buttons' focusability, there is no "next" focusing -
random or algorithmic.
 

Attachments

  • scanner.zip
    9 KB · Views: 4
  • scanner.png
    scanner.png
    6.6 KB · Views: 4
  • scanner2.png
    scanner2.png
    7.8 KB · Views: 4
  • scanner3.png
    scanner3.png
    8 KB · Views: 4
Upvote 0
Top