Scan Barcode And QRcode Using ZXing Android Studio Programmatically

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

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

Download Source Code

Creating scan barcode and qrcode using zxing android 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.

Follow this tutorial for ZBar: Scan Barcode QRcode Android

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 file:

Update 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 scan barcode qrcode scanner android

new activity


Step 7: Updating file:

Add below code to

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.

Kotlin Version

Kotlin version of this tutorial : Android Kotlin Scan Barcode QRCode ZXing

We will use same library but will integrate it with kotlin.

So it was all about scan barcode and qrcode using zxing android 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...

23 Responses

  1. Andrew says:

    Easy scanning. Thanks!

  2. Wai Lay says:

    I cann’t start scan view in DrawerActivity .Please help me.

  3. WQ Yew says:

    Hi, I am unable to start my scanActivity.

    • Hardik Parsania says:

      Is your app crashing?

      • WQ Yew says:

        I realised that my MinSDKTarget was too high which caused my app to crash. So i have fixed that problem. On the other hand, is the scanner ‘scan’ time slow? I tried scanning some sample qr codes. for some codes, the scanner took a short time to decode, while for others, it took a long time, some even went for more than a minute long! Please help. Thanks

  4. WQ Yew says:

    Hi! I am unable to connect to my camera to start the ScanActivity java class. I have already added the camera permission in the manifest. Please help me. Thanks.

  5. Sary says:

    Hello I am news Android learner and I want to know How to open webview by this scan?

  6. Alex says:

    Hi when I press the scan button it only opens the activity_scan.xml file and doesn’t start the camera. I have done the steps exactly as shown. How can I get the button to start the camera?

  7. pranish says:

    Great tutorial. But what I have found is the qr scanned works a bit slow compared to zxing’s app. Whats the reason?

  8. meowww says:

    So Great ~~~ It is work very very well ~~ Thank you so much

  9. Muhammad Ayaz says:

    thanks it work perfect but i want the url text in the scan activity not on the main activity. how can i do that ?thank IA

  10. Mathews Augusto says:

    I’m Brazilian, I do not speak much English, but thank you for posting, thank you very much, that helps me a lot.

  11. Walter Suarez says:

    Hello, at my app the camera is working fine, but i have two button open the camera, this two button working fine too, but when use 1 button erase de info capture in button 2 and viceversa

  12. Muhammad Umair says:

    scanning. Thanks

  13. Burak Çaparlar says:

    do we have a chance to open the camera in a layout what do you think about this stuation ?

Leave a Reply

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