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:
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
}
}
}
}
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"