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.

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

  • 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.

Download Source Code

[sociallocker]Download Kotlin_ScanXing Example[/sociallocker]

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...

15 Responses

  1. mudit says:

    Thanks Bro !! Only Because Of You now i can scan barcodes easily without any problem !!

  2. blah bloh says:

    companion object {
    var tvresult: TextView? = null

    tvresult variable init caused “Do not place Android context classes in static fields, ..bla..bla.” warning
    any chance to call function on MainActivity from handleResult function along with the scan result ?

    • Hardik Parsania says:

      Make function of MainActivity as public in java or in kotlin as lateinit.

      • Akjack says:


        sorry, what function? i don´t know where to put lateinit. 🙁
        Actually, when i take the image, the app crash.

        at com.maia.gitcodigos2.ScanActivity.handleResult(ScanActivity.kt:38)

  3. Pranay says:

    I don’t want to display scanner in different screen, i want to use an overlay for scanner along with other views such as textview and buttons visible to user. How can we do it using ZXing?

  4. Alex says:

    HELP! I literally used your code like it is and it doesn’t work.. Opens the Scanner_activity in blank

  5. Tom Huskey says:


    I tried to download and import the kotlin_ScanXing folder/library but got this error on the Gradle clean/build;

    The Android Gradle plugin supports only Kotlin Gradle plugin version 1.2.51 and higher. Project ‘Kotlin_ScanXing’ is using version 1.2.20.

    Can you help?

  6. MohammadAli says:

    this error come when i copy your code and Re-build my project

    One type argument expected for class Result

    In below method:

    override fun handleResult(rawResult: Result) {



    • MohammadAli says:

      this error solve now but whenever i open Scanner then it’s blank white screen. But when i download your code and run it then it works.
      i copy your code as it is. But I don’t know why this didn’t work in my project.

  7. MOISE says:

    Hi, thanks for tuto
    in my project,i have a problem when i push my button, in LogCat, ihave :
    E/Camera: Camera new cameraInitNormal:-13
    and the camera don’t start.
    if i compile your project, i have :
    E/Camera: Camera new cameraInitNormal:0
    the camera start and the write codebar is ok.
    Have you any idea ?
    See you

Leave a Reply

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