Android: how to know which button was clicked inside "onActivityResult" method - android

I have two buttons, both of them does barcode scanning. Once it returns from scanning, I want to know which button the user clicked so I can take different path on "onActivityResult" method. How do I do that?
I can find out which button was clicked inside onClick() method but that won't help.
public class ReaderActivity extends AppCompatActivity {
private Button scan_btn, verifyButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
scan_btn = (Button) findViewById(R.id.scan_btn);
verifyButton = (Button) findViewById(R.id.buttonVerify);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("LINE UP BARCODE WITH SCANNER");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null){
if(result.getContents()==null){
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, result.getContents(),Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}

You can add boolean value inside your activity
eg :
private boolean isScanButtonClicked;
Use below code
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isScanButtonClicked = true; // true a scan button clicked
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isScanButtonClicked = false; // false as scan button not clicked
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("LINE UP BARCODE WITH SCANNER");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
According to isScanButtonClicked value you can perform your action inside onActivityResult()
Hope these help you.

You could add some flag/data to Intent data return and process this data into onActivityResult to get the button clicked in your action, or change the value of resultCode to process this result on correct way.

Resolved it through get set method
Created a class like below
public class myApplication extends Application{
private String someVariable;
public String getWhichButton() {
return someVariable;
}
public void setWhichButton(String someVariable) {
this.someVariable = someVariable;
}
}
Make sure to add android:name=".myApplication" in the <Application> tag in AndroidManifest. Then in my ReaderActivity during the button click action, I set
((myApplication) activity.getApplication()).setWhichButton("scan");
OR
((myApplication) activity.getApplication()).setWhichButton("Verify");
depending on which button I click. On the onActivityResult method I use
((myApplication) this.getApplication()).getWhichButton();
to retrieve which button was clicked to take action accordingly.
public class ReaderActivity extends AppCompatActivity{
private Button scan_btn, verifyButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
scan_btn = (Button) findViewById(R.id.scan_btn);
verifyButton = (Button) findViewById(R.id.buttonVerify);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((myApplication) activity.getApplication()).setWhichButton("scan");
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((myApplication) activity.getApplication()).setWhichButton("verify");
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("LINE UP BARCODE WITH SCANNER");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null){
if(result.getContents()==null){
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, result.getContents(),Toast.LENGTH_LONG).show();
String val = ((myApplication) this.getApplication()).getWhichButton();
if(val=="scan") {
Toast.makeText(this, "Scan button pressed", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(this, "Verify button pressed", Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}

public class MainActivity extends AppCompatActivity {
public Button btnOne;
public Button btnTwo;
public String btnClicked;
public static int VALUEONE = 1;
public static int VALUETWO = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUm = (Button)findViewById(R.id.btnUm);
btnDois = (Button)findViewById(R.id.btnDois);
btnOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent test = new Intent(MainActivity.this, NextActivity.class);
btnClicked = "btnOne";
startActivityForResult(test,VALUEONE);
}
});
btnTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent test = new Intent(MainActivity.this, NextActivity.class);
btnClicked = "btnTwo";
startActivityForResult(test,VALUETWO);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("Btn clicked: " + btnClicked);
}
...........
I try this code in my Android Studio and it works. You made something similar to this?

Related

How Can I get QR code result to TextView?

I try to get QR code result to textView.
Although the camera is functioning the scan process is not supporting,although the function is inappropriate .an error message is not being display... What I do for that?
This is my QrActivity.....
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class QrActivity extends AppCompatActivity {
/**
* QR code declaration
*/
Button btnscan, btnSearch;
static TextView lblSearch;
#Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
btnscan = (Button) findViewById(R.id.btnscan);
btnscan.setClickable(false);
btnSearch = (Button) findViewById(R.id.btnSearch);
btnSearch.setClickable(false);
lblSearch = (TextView) findViewById(R.id.lblSearch);
final Activity activity = this;
btnscan.setOnClickListener(new View.OnClickListener() {
#Override
public
void onClick(View v) {
btnscan.setClickable(true);
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
}
/**
* ---------------------------------------------------- QR code ----------------------------------------------------------
*/
#Override
protected
void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_SHORT).show();
} else {
/**
* Qr code result
* */
lblSearch.setText(result.getContents());
// Toast.makeText(this, result.getContents(), Toast.LENGTH_SHORT).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
/**
* -----------------------------------------QR scan-- Finished-------------------------------------*
* */

return back data to another activity

i have two activities A and B i am using a button to be directed to activity B i managed to write a code so it code be turned back to activity A automatically without any buttons after 5 seconds
but now i want when it return back to return data to A how it can be done
i wrote the code in A to recieve data but don't know what to add in activity B
Activity A ::
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button two = (Button) findViewById(R.id.button2);
Recieve = (TextView) findViewById(R.id.textView4);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Intent Intent = new Intent(MainActivity.this, Gps.class);
//startActivity( Intent);
sendMessage();
}
});
}
public void sendMessage() {
Intent intent = new Intent(MainActivity.this, Hello.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Response = data.getStringExtra("key");
Recieve.setText("msg is " + Response);
}
}
Activity B::
public class Hello extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
final Intent i = new Intent(Hello.this, MainActivity.class);
Hello.this.startActivity(i);
Hello.this.finish();
}
}, 5000);
}
}

How to take the QR scanner result and make it into a string?

How do I take the 'this' value ( from the 6th line) and make it into a string?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrscanner);
fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample);
fragment.setScanResultHandler(this);
btn = ((Button)findViewById(R.id.scan));
btn.setEnabled(false);
}
For example instead of just toasting the value, can I use the value and make it into a string and then carrying it into another activity.
#Override
public void scanResult(ScanResult result) {
btn.setEnabled(true);
Intent intent = new Intent(QrscannerActivity.this,ProductInfoActivity.class);
startActivity(intent);
Toast.makeText(this, result.getRawResult().getText(), Toast.LENGTH_LONG).show();
}
You should use the Google ZXing Barcode Scanner library and with the below code:
extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HandleClick hc = new HandleClick();
findViewById(R.id.butQR).setOnClickListener(hc);
findViewById(R.id.butProd).setOnClickListener(hc);
findViewById(R.id.butOther).setOnClickListener(hc);
}
private class HandleClick implements OnClickListener{
public void onClick(View arg0) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
switch(arg0.getId()){
case R.id.butQR:
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
break;
case R.id.butProd:
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
break;
case R.id.butOther:
intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR");
break;
}
startActivityForResult(intent, 0); //Barcode Scanner to scan for us
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
TextView tvStatus=(TextView)findViewById(R.id.tvStatus);
TextView tvResult=(TextView)findViewById(R.id.tvResult);
if (resultCode == RESULT_OK) {
tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
} else if (resultCode == RESULT_CANCELED) {
tvStatus.setText("Press a button to start a scan.");
tvResult.setText("Scan cancelled.");
}
}
}
}

Customized Camera view in ZXING code reader to show detection

I am using the ZXING QR Code Reader. It is working fine. Detecting codes etc but it does not shows any detection progress(Like red line) while it is scanning the QR Code. Here is Screenshot
And my ScanActivity is,
ScanCode.Java
public class ScanCode extends Activity implements OnClickListener {
Button btnScan;
TextView scanResult;
String text;
Bitmap bmp;
ImageView ivPic;
#Override
public void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.scan_code);
initViews();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.btnScanCode) {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 1);
}
}
private void initViews() {
scanResult = (TextView) findViewById(R.id.scanResult);
ivPic = (ImageView) findViewById(R.id.capturedImg);
btnScan = (Button) findViewById(R.id.btnScanCode);
btnScan.setOnClickListener(this);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
if (resultCode == Activity.RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
String format = data.getStringExtra("SCAN_RESULT_FORMAT");
Toast.makeText(getApplicationContext(), contents,
Toast.LENGTH_SHORT).show();
scanResult.setText(contents);
}// if result_ok
}// onActivityResult
}
I know this answer is bit late but I've just used Zxing barcode/QR-Code scanner recently in my app, if anyone has issue implementing it just follow the steps below (Using Android Studio Official IDE for android).
First add dependencies in app --> build.gradle file
dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.2.0#aar'
compile 'com.google.zxing:core:3.2.1'
}
Activity Usage:
public class MainActivity extends AppCompatActivity {
Button btnScan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnScan = (Button) findViewById(R.id.btnScan);
btnScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
scanBarcode();
}
});
}
private void scanBarcode()
{
new IntentIntegrator(this).initiateScan();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
}
}
}
If you want to use custom callback and BarCodeView. Zxing provide this functionality simply make layout file
<com.journeyapps.barcodescanner.CompoundBarcodeView
android:id="#+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.journeyapps.barcodescanner.CompoundBarcodeView>
<TextView
android:id="#+id/tvScanResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan Results will be shown here"
android:textColor="#android:color/white"
android:textStyle="bold"/>
In Activity use the following code
public class MainActivity extends AppCompatActivity
{
CompoundBarcodeView barcodeView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barcodeView = (CompoundBarcodeView) findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(callback);
barcodeView.setStatusText("");
}
#Override
protected void onResume()
{
super.onResume();
barcodeView.resume();
isScanned = false;
}
#Override
protected void onPause()
{
super.onPause();
barcodeView.pause();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
private BarcodeCallback callback = new BarcodeCallback()
{
#Override
public void barcodeResult(BarcodeResult result)
{
if (result.getText() != null)
{
barcodeView.setStatusText(result.getText());
tvscanResult.setText("Data found: " + result.getText());
}
//you can also Add preview of scanned barcode
//ImageView imageView = (ImageView) findViewById(R.id.barcodePreview);
//imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
}
#Override
public void possibleResultPoints(List<ResultPoint> resultPoints)
{
System.out.println("Possible Result points = " + resultPoints);
}
};
}
Source zxing-android-embedded. Hope someone get help from this solution

can't call onactivity result function after child activity is finished

i am using following codes.
addNewReceipt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), AddReceipt.class);
myIntent.putExtra("receiptList", receipts);
startActivityForResult(myIntent, RECEIPT_ADD);
}
});
}
#SuppressWarnings("unchecked")
public synchronized void onActivityResult(final int requestCode, int resultCode, final Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
if (requestCode == RECEIPT_ADD)
{
receipts = (ArrayList<Receipt>) data.getSerializableExtra("receiptList");
addReceiptsInListView();
}
}
}
The code for AddReceipt class is as follow,
#Override
public void finish() {
Intent data = new Intent();
data.putExtra("receiptList", receipts);
setResult(RESULT_OK, data);
super.finish();
}
#SuppressWarnings("unchecked")
public void onCreateCall()
{
done.setOnClickListener(new OnClickListener()
{ //receiptAddBtn
#Override
public void onClick(View v)
{
String error = "";
Receipt receipt = new Receipt();
receipt.comments = comments.getText().toString();
receipt.referenceNo = receiptNo.getText().toString();
receipt.image = imageSelected;
if (receipt.image == null || receipt.referenceNo == "" )
{
error = "Please input Receipt No. and attach Image";
displayAlert(error);
}
else
{
receipts.add(receiptCounter,receipt);
finish();
}
}
});
}
The problem is, when the activity is finished... and i pass this receipt, in my previous class from which i call this activity public synchronized void onActivityResult Never works.
The back button is not ending activity either.
Please tell me where i am wrong,.
Best Regards
Here is the solution to your problem
#Override
public void finish() {
Intent data = new Intent();
data.putExtra("receiptList", receipts);
setResult(Activity.RESULT_OK, data);
super.finish();
}

Categories

Resources