Search code examples
androidexternalinterface

android- get data as it enters from external keyboard


I have a bluetooth bar code scanner connected to my tablet. Whenever i scan a barcode, and the focus is on the edit text box of my app, the scanned data appears in the edit text.

I want to take this data simultaneously as it comes in the edit text. I have tried using setOnClickListener,setOnKeyListener . If any one knows, please let me know.

EDITED: Steps that I am following:

  1. my program has a simple edit text, a button, a text box.

  2. on scan of the barcode through a hardware input, the data is getting inserted into my program's edit text.

  3. on click of the button, i copy the edit text contents into text box.

What I want my app to do:

As soon as the data appears in the edit text, i want to copy it into the text box. Right now I am doing it on the button's click.

Here is my code: I doubt if it's helpful, coz the external hardware is displaying the data in the edit text itself.

public class SimpleTextBoxActivity extends Activity {

Button btnClear, btnPairedList, btnAvailableList, btnPairedAvailableList,btnShowScan;

EditText edtSacnnedData;
BroadcastReceiver brSent;
TextView txtShowScannedData;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    edtSacnnedData=(EditText) findViewById(R.id.edtData);
    btnClear=(Button) findViewById(R.id.btnClear);
    btnShowScan=(Button) findViewById(R.id.btnScannedText);
    txtShowScannedData=(TextView) findViewById(R.id.txtScanData);

    Log.d("my", "b4 set visibility");
    edtSacnnedData.setBackgroundColor(Color.BLACK);

    Log.d("my", "after set visibility");
    btnClear.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
            edtSacnnedData.setText("");
            txtShowScannedData.setText("");
        }
    });
    btnShowScan.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String str=edtSacnnedData.getText().toString();
            txtShowScannedData.setText(str);
            edtSacnnedData.setText("");
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();      
}

}


Solution

  • You need to use addTextChangedListener(..) may be helpful for you. Here is link for interesting discussion on this. Text changed listener