Scan Barcode And QRcode Using Zxing Android Kotlin Programmatically

In scan barcode and QRcode using zxing Android Kotlin example, we will learn how to scan the barcode and QRcode using a zxing library in Android studio.

We need to implement scan feature when developing apps for ecommerce or grocery stores.

First of all check output of this scan barcode and QRcode using zxing android kotlin tutorial then we will implement it programmatically.

Download Source Code

Creating scan barcode and qrcode using zxing android kotlin Example

Step 1: Create a new project in Android Studio.

Step 2: Updating build.gradle(Module:app) file

add following code into dependencies{}

Here we are using ZXing library. You can also use ZBar library.

Github link for ZBar or ZXing library: ZXing or ZBar

Step 3: Updating AndroidManifest.xml file

add camera permission between <manifest>….</manifest> tag.

Note: If you are targeting SDK version above 22 (Above Lollipop)  then you need to ask a user for granting runtime permissions. Check marshmallow runtime permission for more information.

Step 4: Updating activity_main.xml file:

Copy following code into android_main.xml file:

Step 5: Updating MainActivity.kt file:

Update MainActivity.kt as per below code:

Here we will open ScanActivity on the button’s click.

Step 6: Opening new activity in android studio

To open new activity in the android studio, click on File tab which is present at the left top bar.

Now go to File->New->Activity->Empty Activity and place name of activity as ScanActivity.

zxing barcode qrcode scanner android kotlin

New Activity

Step 7: Updating ScanActivity.kt file:

Add below code to ScanActivity.kt

Note: We do not need to update activity_scan.xml file because we will open camera preview here so no need to do anything in layout file.

Step 8: Brief Description of ScanActivity.java

  • In onCreate() method, we have set the view as scannerview instead of xml file layout.
  • In onResume() method, we have set the resulthandler and started camera preview.
  • In onPause() method, we are stopping camera preview.

Step 9: Explaining handleResult() method

  • As you can see in above code, we get result object which contains resulting Barcode or QRcode after scanning is completed.
  • We will get final result by running result.getText() method and we have set it into TextView of MainActivity.

Java Version

Java version of this tutorial : Android Scan Barcode QRCode ZXing

So it was all about scan barcode and qrcode using zxing android kotlin example programmatically. Thank you.

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *