How to Create a Button that on Pressed Scan the QR Code and Displays It on the Alert Box and in a Text View?


BarcodeBC > Articles > How to Create a Button that on Pressed Scan the QR Code and Displays It on the Alert Box and in a Text View?


Create a Button that on Pressed Scan the QR Code and Displays It on the Alert Box and in a Text View

Here's an example of how to create a button in Android that, when pressed, scans a QR Code using the device camera, displays the scanned QR Code in an alert box, and also displays it in a text view

1. Add the ZXing Android Embedded library to your project's dependencies. You can do this by adding the following to your app-level build.gradle file.


dependencies {
    implementation 'com.journeyapps:zxing-android-embedded:4.2.0'
    implementation 'com.google.zxing:core:3.4.1'
}

2. In your layout XML file, add a button and a text view.


<Button
    android:id="@+id/button_scan_qr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Scan QR Code" />

<TextView
    android:id="@+id/text_qr_code"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge" />

3. In your activity or fragment, get references to the button and text view, and set an OnClickListener on the button to initiate the QR Code scan.


public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_SCAN_QR = 1;

    private Button buttonScanQr;
    private TextView textQrCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonScanQr = findViewById(R.id.button_scan_qr);
        textQrCode = findViewById(R.id.text_qr_code);

        buttonScanQr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
                integrator.setPrompt("Scan a QR Code");
                integrator.setOrientationLocked(false);
                integrator.initiateScan();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SCAN_QR) {
            IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if (result != null) {
                if (result.getContents() == null) {
                    Toast.makeText(this, "Scan canceled", Toast.LENGTH_SHORT).show();
                } else {
                    String qrCode = result.getContents();
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage(qrCode);
                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.show();
                    textQrCode.setText(qrCode);
                }
            }
        }
    }
}

4. Override the onActivityResult() method to receive the scan result. When the result is received, show an alert dialog with the scanned QR Code, and set the text view to display the scanned QR Code as well. You can also customize the behavior of the scan using the IntentIntegrator class. In this example, we set a prompt for the scan and allow the user to rotate their device during the scan



Why does the alert box display the previously set text in the alert box rather than the newly scanned QR Code text?

It sounds like you are reusing the same instance of the alert dialog box and not updating its content when a new QR Code is scanned. To solve this, you can try the following approach.

1. Create a new instance of the alert dialog each time a QR Code is scanned.

2. Set the text of the alert dialog box with the scanned QR Code text.

3. Show the alert dialog box to the user.

Here is an example code snippet that demonstrates this approach


public class MainActivity extends AppCompatActivity {

    private Button scanButton;
    private TextView qrCodeTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scanButton = findViewById(R.id.scan_button);
        qrCodeTextView = findViewById(R.id.qr_code_text_view);

        scanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Create a new instance of the alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                // Create a new instance of the QR Code scanner
                QRCodeReaderView qrCodeReaderView = new QRCodeReaderView(MainActivity.this);

                // Set the content of the alert dialog to be the QR Code scanner
                builder.setView(qrCodeReaderView);

                // Set a listener for when a QR Code is scanned
                qrCodeReaderView.setOnQRCodeReadListener(new QRCodeReaderView.OnQRCodeReadListener() {
                    @Override
                    public void onQRCodeRead(String text, PointF[] points) {
                        // Set the text of the alert dialog box with the scanned QR Code text
                        builder.setMessage(text);

                        // Set a listener for when the user clicks "OK" on the alert dialog box
                        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                // Update the text view with the scanned QR Code text
                                qrCodeTextView.setText(text);
                            }
                        });

                        // Show the alert dialog box to the user
                        builder.show();
                    }
                });

                // Start the QR Code scanner
                qrCodeReaderView.startCamera();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Stop the QR Code scanner when the activity is paused
        qrCodeReaderView.stopCamera();
    }
}

This code creates a new instance of the alert dialog box each time a QR Code is scanned and sets its content to be the QR Code scanner. When a QR Code is scanned, it sets the text of the alert dialog box with the scanned QR Code text, sets a listener for when the user clicks "OK" on the alert dialog box to update the text view with the scanned QR Code text, and shows the alert dialog box to the user.