Ovidiu Ovidiu - 22 days ago 7
Android Question

Custom Keyboard - capturing barcode scan results

Ok, so here is the situation:
I am working with a keyboard (https://github.com/AnySoftKeyboard/AnySoftKeyboard) and a barcode scanner (https://github.com/dm77/barcodescanner). When I press a specific button on the keyboard, it opens the barcode scanner, after it scans, it returns to the previous screen (let's say messaging app) with the result.
All fine and dandy, except for the fact that I can't insert the result in the EditText, because the input connection "getCurrentInputConnection()" is different from the initial one (before launching the barcode scanner activity), because it's another instance (this happens because I launch a barcode scanner activity and after I'm returning to the initial screen (messaging app with the keyboard shown and the EditText focused).
Here is the code for inserting in any EditText from any app:

InputConnection inputConnection = getCurrentInputConnection();
inputConnection.setComposingText(scanResult, 1);


Any ideas on how to solve this ?

Answer Source

So, I've managed to solve this (for anyone who faces this issue), the problem was, when I received the result (in the previous screen), I had to make it wait 1 second (with runnable and handler - postDelayed), and then do getCurrentInputConnection() and paste the result inside.