1

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

}

PrincessLeiha
  • 3,144
  • 4
  • 32
  • 53

3 Answers3

2

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

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
0

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.

Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38
  • the data appears on it's own, I am not doing any thing extra for it. It's the hardware's property to insert the scanned data into some editable text box. – PrincessLeiha Jan 04 '12 at 07:06
  • But app is yours. Its code that is showing data on that particular edittext , hardware role is just to deliver the bytes to android phone but not to show on any edittext itself. If you can share your app code, I will point out what I am saying. – Sourab Sharma Jan 04 '12 at 07:10
  • have posted the code... I am not doing any thing at all... the hardware is designed in such a way that it enters the data straight into the edit text. – PrincessLeiha Jan 04 '12 at 07:39
  • You are using bluetooth, then you must have some bluetooth service in your code which enable you to connect to device. In that bluetooth service method there will be a case like Message_Read which is setting text on your editText. – Sourab Sharma Jan 04 '12 at 07:57
  • If your code can be shared publicly, give me your project code, I will fix it, it will be 2 mins of work. – Sourab Sharma Jan 04 '12 at 07:57
  • THIS IS MY ONLY CODE. THERE IS NO OTHER CODE!!! The hardware provider have already provided an app which pairs the hardware to my tablet and then it works as an external key board. – PrincessLeiha Jan 04 '12 at 09:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6373/discussion-between-pallavi-and-sourab-sharma) – PrincessLeiha Jan 04 '12 at 09:33
0

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.

Suleman Khan
  • 634
  • 1
  • 10
  • 23