How Do You Integrate a QR Code With an Android App?

Android, Android Apps

Integrating a QR code with an Android app can open up a world of possibilities for enhancing user experiences and streamlining processes. QR codes, or Quick Response codes, are two-dimensional barcodes that can be scanned using a smartphone’s camera.

They can contain various types of information, such as URLs, text, or contact information. In this tutorial, we will explore how to integrate a QR code with an Android app.

Step 1: Adding the Required Dependencies

To begin with, we need to add the necessary dependencies to our Android project. Open your project’s build.gradle file and add the following lines of code inside the dependencies block:

implementation ‘’
implementation ‘com.journeyapps:zxing-android-embedded:3.0’

These dependencies will enable us to generate and scan QR codes within our Android app.

Step 2: Creating a QR Code Generator

Now that we have added the required dependencies, let’s start by creating a QR code generator in our Android application. Create a new Java class called Inside this class, add the following code:

import android.util.Log;

import com.zxing.BarcodeFormat;
import com.EncodeHintType;
import com.WriterException;
import com.common.BitMatrix;
import com.journeyapps.barcodescanner.BarcodeEncoder;

import java.HashMap;
import java.Map;

public class QRCodeGenerator {

    public Bitmap generateQRCode(String data) {
        Map hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        try {
            BitMatrix bitMatrix = new BarcodeEncoder().encodeBitmap(data, BarcodeFormat.QR_CODE, 500, 500, hints);
            return Bitmap.createBitmap(bitMatrix.getWidth(), bitMatrix.getHeight(), Bitmap.Config.RGB_565);
        } catch (WriterException e) {
            Log.e("QRCodeGenerator", "Error generating QR code: " + e.getMessage());

        return null;

This code uses the ZXing library to generate a QR code bitmap from the given data. The generateQRCode method takes a string parameter and returns a bitmap.

Step 3: Displaying the QR Code

Now that we have our QR code generator ready, let’s move on to displaying the generated QR code in our Android app. Open the layout file where you want to display the QR code (e.g., activity_main.xml) and add an ImageView element:

    android:layout_gravity="center" />

Next, open the corresponding Java class (e., and add the following code inside the onCreate method:

QRCodeGenerator qrCodeGenerator = new QRCodeGenerator();
Bitmap qrCodeBitmap = qrCodeGenerator.generateQRCode("");

ImageView qrCodeImageView = findViewById(;

This code creates an instance of our QRCodeGenerator class and generates a QR code bitmap using a sample URL. Finally, it sets the generated bitmap as the image resource for the ImageView.

Step 4: Scanning QR Codes

To complete our integration, let’s add the functionality to scan QR codes within our Android app. Create a new Java class called QRCodeScannerActivity.content.Intent;
import android.os.Bundle;
import android.Log;

import androidx.annotation.Nullable;

import com.Result;
import com.CaptureManager;
import com.CompoundBarcodeView;

public class QRCodeScannerActivity extends AppCompatActivity implements CompoundBarcodeView.TorchListener {

private CaptureManager captureManager;
private CompoundBarcodeView barcodeScannerView;

protected void onCreate(Bundle savedInstanceState) {

barcodeScannerView = findViewById(R.barcode_scanner_view);

captureManager = new CaptureManager(this, barcodeScannerView);
captureManager.initializeFromIntent(getIntent(), savedInstanceState);

protected void onResume() {

protected void onPause() {

protected void onDestroy() {

protected void onSaveInstanceState(Bundle outState) {

public CompoundBarcodeView getBarcodeScannerView() {
return barcodeScannerView;

public void onTorchOn() {
// Handle torch on event

public void onTorchOff() {
// Handle torch off event

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

captureManager.onActivityResult(requestCode, resultCode, data);

public void handleDecode(Result result) {
// Handle the scanned QR code result here
Log.d(“QRCodeScannerActivity”, “Scanned QR code: ” + result.getText());

This code sets up a basic QR code scanning activity using the ZXing library. It initializes the barcode scanner view and handles various lifecycle events.


By following these steps, you can seamlessly integrate QR codes into your Android app. You have learned how to generate a QR code using the ZXing library and display it in an ImageView. Additionally, you have implemented a basic QR code scanning activity using the same library.

QR codes can be used in various scenarios like sharing URLs, storing textual information, or even facilitating contact information exchange. Experiment with different use cases and explore the possibilities of integrating QR codes into your Android app to enhance user experiences and simplify processes.