-3

I have added Zxing 3.0.1 as a library to my project I have 2 layouts: on first i have button which call CaptureActivity of Zxing library, and when CaptureActivity decode a barcode i need to put this code on a second layout into textView. How can i do this? If someone know, please write step-by-step manual, because in and Android beginner, and dont know what to code .

I must put something this part of code CaptureActivity?

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
  if (requestCode == HISTORY_REQUEST_CODE) {

    int itemNumber = intent.getIntExtra(Intents.History.ITEM_NUMBER, -1);
    if (itemNumber >= 0) {
      HistoryItem historyItem = historyManager.buildHistoryItem(itemNumber);
      decodeOrStoreSavedBitmap(null, historyItem.getResult());
    }
  }
}
}
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Kirill
  • 169
  • 1
  • 9
  • 1
    your question isn't clear. what do you mean by 2 layouts? Did you mean to say 2 activities, one with a button and one with textview? – Sharjeel Jan 15 '15 at 15:38
  • The answer depends on if you you want to go back to the previous activity with the result or if you want to start a new activity with the result. – Shivam Verma Jan 15 '15 at 15:42

1 Answers1

1

Yout have to start CaptureActivity with startActivityForResult:

startActivityForResult(new Intent(this, CaptureActivity.class), CaptureActivity.REQUEST_CODE);

When Zxing done with decoding (in my case it return it's result in handleResult(Result rawResult) callback- you should call setResult(RESULT_OK, data) inside CaptureActivity where data - is your decoded bundled string and call finish():

@Override
public void handleResult(Result rawResult) {
    Bundle data = new Bundle();
    args.putString(RESULT_KEY, rawResult.getText());
    setResult(RESULT_OK, data);
    finish();
}

In Activity which started CaptureActivity you have to override onActivityResult(int requestCode, int resultCode, Intent data) callback and handle getting result - in your case - update a TextView:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
   if (requestCode == CaptureActivity.REQUEST_CODE && resultCode == RESULT_OK) {
       youtTextView.post(new Runnable() {
           @Override
           public void run() {
               yourTextView.setText(intent.getStringExtra(RESULT_KEY));
           }
       };
   }
}
localhost
  • 5,568
  • 1
  • 33
  • 53
  • On my MainActivity i add a button with method onClick(where i start CaptureActivity with startActivityForResult).I still dont understand what code i must put in CaptureActiviti, and what i need to write in my MainActivity in method onActivityResult – Kirill Jan 16 '15 at 12:09
  • @Kirill, will update my answer with code from my app shortly. – localhost Jan 16 '15 at 12:33
  • i didnt understand code of **CaptureActivity** where zxing done with decocing there? where i must put handkeResult? – Kirill Jan 17 '15 at 11:40