PrincessLeiha
PrincessLeiha

Reputation: 3174

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();      
}

}

Upvotes: 1

Views: 1462

Answers (3)

Suleman Khan
Suleman Khan

Reputation: 634

Give some id's to your UI design and do the work. I'm showing you an example.

 EditText editText;
 TextView textView;

 editText = (EditText) findViewById(R.id.editText);
 textView = (TextView) findViewById(R.id.textView);

 editText.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            textView.setText(editText.getText().toString());

        }
    });

In the above example as soon as you move your focus your textview will be updated or else you can also use:

editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            textView.setText(editText.getText().toString());

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

This is just an example. You can give your own id's and names.

Upvotes: 0

kosa
kosa

Reputation: 66657

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

Upvotes: 2

Sourab Sharma
Sourab Sharma

Reputation: 2960

Try to find that editText.setText(data) code in your class.

Whatever will be in your parenthesis of setText() method of that editText, that is your data and you can use it from here.

If my assumption is wrong, please share your code.

Upvotes: 0

Related Questions