Search code examples
androidbarcodebarcode-scanner

How to make plain text which appears after scanning a QR clickable and redirects the user to the browser?


I have written a piece of code for scanning QR code. But after scanning it is showing the data in plain text even if it is a link or URL to some website. I want it to redirects the user to the particular website after scanning directly in the browser.

public void handleResult(Result rawResult) {
        // Do something with the result here
       // Log.v("tag", rawResult.getText()); // Prints scan results
       // Log.v("tag", rawResult.getBarcodeFormat().toString()); 
// Prints the scan format (qrcode, pdf417 etc.)

        MainActivity.tvresult.setText(rawResult.getText());
        onBackPressed();

        // If you would like to resume scanning, call this method below:
        //mScannerView.resumeCameraPreview(this);
    }

Solution

  • Put this attribute to your TextView

    <Textview 
              android:autoLink="web"/>
    

    Other option is to create an Intent for that when user click on your TextView

    Uri uri = Uri.parse(rawResult.getText());
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent)
    

    Better way also can be create a Linkify

    Linkify.addLinks(tvresult, Linkify.WEB_URLS);
    

    You can choose what you want.