title: EditText focus disappears, when the barcode scanner reads the barcode
body: <p>I need to read a barcode for an application. I am using trigger barcode scanner for this. Communication is via USB.</p>
  
  <p>As you know, barcode scanners work like keyboards. When the device reads the barcode, it tries to write the value to the input that has focus. And the user presses the trigger, the barcode scanner works until 
  the barcode is read successfully. Then it takes itself in standby mode. Ideal way to read a lot of barcodes.</p>
  
  <p>The problem is that after the user presses the trigger and reads the barcode, the focus of the EditText disappears. Focus is going to another view in the same layout randomly. When the user tries to read one more barcode, the operation fails because there is no focus on related EditText.</p>
  
  <p><strong>What did I try?</strong></p>
  
  <pre><code>android:windowSoftInputMode="stateAlwaysVisible"
  </code></pre>
  
  <p>Added above line to the manifest file </p>
  
  <pre><code>android:focusable="true"
  android:focusableInTouchMode="true"
  </code></pre>
  
  <p>Added above lines on the xml side. </p>
  
  <pre><code> edtBarcode.setOnFocusChangeListener(new View.OnFocusChangeListener()
      {
          @Override
          public void onFocusChange(View v, boolean hasFocus)
          {
              if (!hasFocus)
              {
                  //find the view that has focus and clear it.
                  View current = getCurrentFocus();
                  if (current != null)
                  {
                     current.clearFocus();
                  } 
  
                  edtBarcode.requestFocus();
              }
          }
      });
  </code></pre>
  
  <p>It detects when EditText loses its focus. But i can't assign it back. I ensure that the EditText is focusable in touch mode.</p>
  
  <p><strong>How did i solve it?</strong></p>
  
  <pre><code>    handlerFocus = new Handler();
      final int delay = 1000; //milliseconds
  
      handlerFocus.postDelayed(new Runnable()
      {
          public void run()
          {
              edtBarcode.requestFocus();
              handlerFocus.postDelayed(this, delay);
          }
      }, delay);
  </code></pre>
  
  <p>I know this solution is not good. So how do I make the focus always stay in the same EditText without opening the keyboard?</p>
  
  <p><strong>CRITICAL EDIT</strong></p>
  
  <p>After 4 years, I realized that the approach above was completely wrong. The better approach here and I do not remove the old one because it can work for someone else.</p>
  
  <p>You should override <a href="https://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent(android.view.KeyEvent)" rel="nofollow noreferrer"><strong>dispatchKeyEvent</strong></a> to intercept all key events before they are dispatched to the window. Be sure to call this implementation for key events that should be handled normally.</p>
  
  <pre><code>    String barcode = "";
  
      @Override
      public boolean dispatchKeyEvent(KeyEvent e)
      {
          if (e.getAction() == KeyEvent.ACTION_DOWN)
          {
              char pressedKey = (char) e.getUnicodeChar();
              barcode += pressedKey;
          }
          if (e.getAction() == KeyEvent.ACTION_UP &amp;&amp; e.getKeyCode() == KeyEvent.KEYCODE_ENTER)
          {
              handleInput(barcode);
              barcode = "";
          }
  
          return false;
      }
  </code></pre>