Search code examples
androidzxing

How to use the ZXING scan result


I'm breaking my head over this one. I'm kind of new in android development.

I want to use the scan result from ZXING. I integrated ZXING into my android app, the scan works ok. Now I want to use the scan result to update a TextView in the main activity.

How can I do that?

My code is doing the following for now:

  1. Open Main activity with a button.
  2. On clicking the button it activates the scanner.
  3. After scanning it goes back to the main activity.
  4. The TextView is empty and for some reason it goes back to the original content after moving from landscape to portrait.

After (2) I want to use the scanned result to update the TextView in the main activity (tv in my code).

Please help guys, Thanks.

My code:

public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button pressToScan = (Button) findViewById(R.id.button1);

    pressToScan.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent data = new Intent("com.google.zxing.client.android.SCAN");
            data.putExtra("SCAN_MODE", "QR_CODE_MODE");
            startActivityForResult(data, 0);

TextView tv = (TextView) findViewById(R.id.scanResult);
                tv.setText(data.getStringExtra("SCAN_RESULT"));
        }
    });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    String contents = null;
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0) {
          if (resultCode == RESULT_OK) {
             contents = data.getStringExtra("SCAN_RESULT");
             String format = data.getStringExtra("SCAN_RESULT_FORMAT");

             // Handle successful scan
          } else if (resultCode == RESULT_CANCELED) {
             // Handle cancel
          }
    }

}

}


Solution

  • Move the following block

    TextView tv = (TextView) findViewById(R.id.scanResult);
    tv.setText(data.getStringExtra("SCAN_RESULT"));
    

    to

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    String contents = null;
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0) {
          if (resultCode == RESULT_OK) {
             contents = data.getStringExtra("SCAN_RESULT");
             String format = data.getStringExtra("SCAN_RESULT_FORMAT");
             //moved here
             TextView tv = (TextView) findViewById(R.id.scanResult);
             tv.setText(contents);
             // Handle successful scan
          } else if (resultCode == RESULT_CANCELED) {
             // Handle cancel
          }
    }
    

    }

    [Edit]

    Add this in your activity

    @Override
    public void onConfigurationChanged(Configuration newConfig){        
        super.onConfigurationChanged(newConfig);
    }
    

    in the manifest change the main activity to add android:configChanges="orientation"

    <activity android:name="..."
              android:label="@string/appName"
              android:configChanges="orientation"