I use a special android device for laser scanning bar codes. It has a special button that reads the barcode and inserts the result into any input field in focus on the screen. I need to be able to accept the processing of results in my application when there are no input fields on the screen. You need something like analog addTextChangedListener for EditText .

  • So how does the text get received before inserting it into the input field (the code would be attached)? Could not intercept this moment in any way? Is there api of the device not working directly with EditText? - Vladimir Parfenov
  • The device already has its api, and regardless of the state of the phone (only if it is not turned off), I can scan the barcode with a special button (the device makes a corresponding sound when it is scanned). There are third-party sdk of the Zebra type where there are all sorts of barcodeListeners , but I first wanted to find out if there is a standard way to receive and process the scan result without any input fields. - Amir Dautov
  • So how do I know how to get the text out of an api that I haven’t seen in my life?) If it works in the background, do you really need to get results from the broadcast receiver, or is there an opportunity to somehow connect to the api and constantly monitor events? If yes, then you can start the service, which will connect to the API and listen in the background all the time. - Vladimir Parfenov
  • Just as I understood here, the action of the standard insert in the input field, and the insert function works even if there are no fields, and I thought maybe you can somehow get the result of this insert using standard methods in Android. - Amir Dautov
  • Nothing is clear. What is the standard insert? The device cannot take and insert some text into your application if the application did not prescribe the necessary permissions / broadcasts / services or simply did not connect to the API in the code. - Vladimir Parfenov

1 answer 1

The standard barcode scanner works on the so-called. keyboard break technologies, when a scanned barcode emulates keyboard input, that is, in fact, for an external program, the barcode scanner is perceived as a keyboard.

But as soon as you want to get a barcode outside the keyboard input field, a problem immediately arises - you need to have access to the scanner API already at the level of the software interface. Immediately after this, a cloud of difficulties begins: a scanner model, a driver, API call signatures, and so on. other I do not think that this is what you need.

You can try the following hack:

  1. Declare an EditText that is invisible, that is, with the android:background="@null" XML attribute android:background="@null" or programmatically: edittext.setBackgroundResource(android.R.color.transparent)
  2. Set the input focus to it via editText.requestFocus()
  3. You hang on him a listener who watches his content
  4. Scan barcode
  5. As soon as the listener reports that the input field is non-empty, copy the scanned text to where you want and immediately clear the field.
  • Thanks, that was necessary. Since I use this edittext in the alertdialog, I wrote this: edittext.setBackground(null); edittext.setBackgroundResource(android.R.color.transparent); edittext.requestFocus(); edittext.setVisibility(View.INVISIBLE); I inserted the INVISIBLE attribute, because after scanning, the result of the scan appeared on the empty field. And one more question, is it possible to make so that this edittext does not occupy a place in the dialogue? - Amir Dautov
  • This is unlikely - try resizing the EditText to its minimum. Set him the size of a small font - as an option - Barmaley
  • How to achieve what would be cleared the previous one, what was scanned in EditText? - Yaroslav Volodymyrovych
  • everything is fine, I got out - Yaroslav Volodymyrovich