android get barcode scanner input without edittext
<p>I have a <strong>physical barcode scanner</strong> and I want to get it's input, i.e the barcode, in the app without having a focused <code>EditText</code>.</p>

<p>I tried adding a <code>KeyListener</code> in my <code>Activity</code>. However, none of its implemented methods (<code>onKeyUp</code>, <code>onKeyDown</code> etc) was called. </p>

<p>Then I added the <code>dispatchKeyEvent</code>, which worked, but is never called as many times as the barcode length. Instead, before the barcode is read, some random <code>button</code> in my view gets focus from the barcode scanner.</p>

<pre><code>String barcode = "";

@Override
public boolean dispatchKeyEvent(KeyEvent e) {
    char pressedKey = (char) e.getUnicodeChar();
    barcode += pressedKey;
    if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
        Toast.makeText(getApplicationContext(), "barcode---&gt;&gt;&gt;" + barcode, Toast.LENGTH_LONG)
                .show();
    }

    return super.dispatchKeyEvent(e);
}
</code></pre>

<p>I've seen a few questions out there in SO but none really gave a concrete answer.</p>