Search code examples
javaandroidqr-codezxing

How to make active URL in QR Scanner result, writen using ZXing fork


I am creating QR Scanner for Android, using fork of ZXing (https://github.com/journeyapps/zxing-android-embedded). Program scans QR codes and Barcodes, but in Alertdialog I get all results in a plain text, even if it's a link. I want program to show urls as an active links in result box, IF it's a link, and show plain text IF it's a barcode.

I watched every single tutorial with this library https://github.com/journeyapps/zxing-android-embedded (Yes it's not compatible with ZXing, so I can't use code related to ZXing, as I get it), I read documentation avalilable, googled it, didn't find anywhere an aswer to this.

Here is the code, which responsible for showing result after scan.

private void scanCode() {
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.setCaptureActivity(CaptureAct.class);
        integrator.setOrientationLocked(false);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
        integrator.setPrompt("Place QR code or Barcode in square");
        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) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(result.getContents());
                builder.setTitle("Result");
                builder.setPositiveButton("Scan Again", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        scanCode();
                    }
                }).setNegativeButton("Quit app", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();

            } else {
                Toast.makeText(this, "No Results", Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

Solution

  • Change your onActivityResult Method like below to show url as active link and text as plain text in Alertdialog from Scan result.

      @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() != null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Scan Result :");
                builder.setTitle("Result");
                builder.setPositiveButton("Scan Again", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        scanCode();
                    }
                }).setNegativeButton("Quit app", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                // added few lines 
                 TextView text = new TextView(this);
                 SpannableString url = SpannableString.valueOf(result.getContents());
                 Linkify.addLinks(url,Linkify.WEB_URLS);
                 text.setText(url);
                 text.setMovementMethod(LinkMovementMethod.getInstance());
                 AlertDialog dialog = builder.create();
                 dialog.setView(text);
                 dialog.show();
    
    
            } else {
                Toast.makeText(this, "No Results", Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }