Search code examples
androidandroid-edittextbarcode-scanner

Different ways to get text automatically from Edittext


Since I'm using scanner, after scanning the barcode into edittext I need to get text from the edittext automatically and check the record for that barcode from the local database.

I know that there is addTextChangedListener for getting the text from the edittext automatically. But there are issues with that like it is checking each character of barcode when scanned instead of checking the barcode string in the local database.


Solution

  • You may use Handler and its methods postDelayed(Runnable r, long delayMillis) and removeCallbacks(Runnable r).

    In your TextWatcher invoke removeCallbacks(...) in order to remove any previous Runnable and invoke postDelayed(...) with a little delay and a Runnable that will check the barcode string. So you may accomplish your search in the database in several milliseconds after the last digit was sent to your EditText.

    Also your scanner may send a special symbol at the end of a barcode. So read its documentation. Probably you don't need Handler, but you need to wait for that special symbol