Search code examples
javaandroidandroid-intentbarcode-scanneronactivityresult

Android Studio Bar Code Scanner App. Scanning Via Intent


I have an android app that has a very simple layout. A button (scan_barcode1) that is followed by an edittext (scan_content) and another button (scan_barcode2) followed by another editext (scan_content2).

     <Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/scanner"
    android:id="@+id/scan_barcode1"
    android:layout_below="@+id/spinner7"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="25dp"
    android:onClick="openScanner1" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/scan_content"
    android:layout_below="@+id/scan_barcode1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="@string/serial_number"
    android:layout_alignRight="@+id/spinner7"
    android:layout_alignEnd="@+id/spinner7" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/scanner"
    android:id="@+id/scan_barcode2"
    android:layout_below="@+id/scan_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="openScanner2" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/scan_content2"
    android:layout_below="@+id/scan_barcode2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="@string/serial_number"
    android:layout_alignRight="@+id/scan_content"
    android:layout_alignEnd="@+id/scan_content" />

WHAT I WANT MY APP TO DO: Upon pressing a button, the scan functionality will open, scan the barcode and place the serial number in the edittext below it. For example if you press scan_barcode1 the serial number will be placed in scan_content and if scan_barcode2 is pressed the serial number will appear in scan_content2.

WHAT MY APP DOES: Upon pressing a button, the scan functionality will open, scan the bar code is placed in both editext in the activity. For example, if I press scan_barcode1, the serial number is placed in scan_content and scan_content2.

The following is the code for the intents that call the bar code scanner. I am using the zxing library fyi.

    //This method opens the barcode scanner when scan_barcode1 button is pressed

    public void openScanner1 (View view) {
    IntentIntegrator scanIntegrator = new IntentIntegrator (this);
    scanIntegrator.initiateScan();
}



    //This method opens the bar code scanner when scan_barcode2 is pressed

    public void openScanner2 (View view) {
    IntentIntegrator scanIntegrator = new IntentIntegrator(this);
    scanIntegrator.initiateScan();
}

I initialize the two editText as contentTxt and contentTxt2

     contentTxt = (EditText) findViewById(R.id.scan_content);
    contentTxt2 = (EditText) findViewById(R.id.scan_content2);

I then begin the OnActivityResult Method which retrieves the edittext and is supposed to place it in the correct edittext (it doesnt)

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

    //Places barcode in the scan_content editext

    if (scanningResult != null) {
        String scanContent = scanningResult.getContents();
        contentTxt.setText(scanContent);
    }

    //places bar code in the scan_content2 edittext
    if (scanningResult !=null) {
        String scanContent = scanningResult.getContents();
        contentTxt2.setText(scanContent);
    }


    else{
        Toast toast = Toast.makeText(getApplicationContext(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
    }

}

Instead the barcodes just replicate across both of the edittext. I have been stuck on this problem for some time and Im desperate for a solution. Any help is greatly appreciated!

So I have updated my code but now instead of retrieving the barcode at all i get null as the result and my toast message pops up. Anyone know what going on? This happens no matter what button I press.

     private int scanFieldPosition = 0;

public void openScanner1 (View view) {
    scanFieldPosition = 1;
    IntentIntegrator scanIntegrator = new IntentIntegrator (this);
    scanIntegrator.initiateScan();

}

public void openScanner2 (View view) {
    scanFieldPosition = 2;
    IntentIntegrator scanIntegrator = new IntentIntegrator(this);
    scanIntegrator.initiateScan();

}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanningResult != null && scanFieldPosition == 1) {
        String scanContent = scanningResult.getContents();
        contentTxt.setText(scanContent);
    }

    if (scanningResult !=null && scanFieldPosition == 2) {
        String scanContent = scanningResult.getContents();
        contentTxt2.setText(scanContent);
    }


    else{
        Toast toast = Toast.makeText(getApplicationContext(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
    }

}

The problem was that I didnt have the variable static. I still get the toast message but my barcode it placed in the right edittext which is strange. Anyways, my app works now thanks to all your help!

    private static int scanFieldPosition = 0;

Solution

  • You can have a static flag set for this, and assign 1 when to it when button1 is pressed and assign 2 to it when button2 is pressed. Then you could check for the flag in your onActivityResult(). You could do something like this:

    public static int editTextNumber = 0;
    
    
    public void openScanner1 (View view) {
    IntentIntegrator scanIntegrator = new IntentIntegrator (this);
    scanIntegrator.initiateScan();
    editTextNumber  = 1;
    }
    
    
    public void openScanner2 (View view) {
    IntentIntegrator scanIntegrator = new IntentIntegrator(this);
    scanIntegrator.initiateScan();
    editTextNumber = 2;
    }
    

    And then in your onActivityResult()

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    
    //Places barcode in the scan_content editext
    
    
    Log.e("editTextNumber",""+ editTextNumber ); // check whats value here
    if (scanningResult != null && editTextNumber == 1) { // checking for flag here
        String scanContent = scanningResult.getContents();
        contentTxt.setText(scanContent);
    }
    
    //places bar code in the scan_content2 edittext
    else if (scanningResult !=null && editTextNumber == 2) {  // checking for flag also
        String scanContent = scanningResult.getContents();
        contentTxt2.setText(scanContent);
    }
    
    
    else{
        Toast toast = Toast.makeText(getApplicationContext(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
       }
    
    }