-1

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);
        }
    }
  • Does this answer your question? [How can I get clickable hyperlinks in AlertDialog from a string resource?](https://stackoverflow.com/questions/1997328/how-can-i-get-clickable-hyperlinks-in-alertdialog-from-a-string-resource) – Janez Kuhar Mar 16 '21 at 18:02
  • Thank you, I almost done, I have link now, did it with Linkify, but it's not clickable, to call setMovementMethod, I need XML file with id, which I don't actually need, thinking how to solve this now... – Оксана OZ Mar 16 '21 at 18:25

2 Answers2

0

I think you need to convert the text result as link. You can use Linkify I think. Set the custom layout for Dialog and add the TextView oh the Layout.

fanjavaid
  • 1,676
  • 8
  • 34
  • 65
0

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);
    }
}
Zahid Islam
  • 740
  • 1
  • 5
  • 11